Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style tag append every time when we navigate api tab while using CDN link #1724

Open
jatingajera31 opened this issue May 14, 2024 · 11 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@jatingajera31
Copy link

What happens?

I am using this scaler CDN link : https://cdn.jsdelivr.net/npm/@scalar/[email protected]
when i navigate other tab and back to swagger api tab every time new style tag is appending and it will impact on performance and speed

image

What did you expect to happen?

The internal style append only once.

How can we reproduce the issue?

1 Setup CDN in Angular project.
2 Setup API documentation page in particular url.
3 Navigate to another page.
4 Go to the existing page on which we loaded the API documentation via CDN link
5 See head tag style in DOM via inspect element in browser

Swagger/OpenAPI Example

No response

@jatingajera31 jatingajera31 added the bug Something isn't working label May 14, 2024
@hanspagel
Copy link
Member

hanspagel commented May 14, 2024

Thanks for bringing this up!

Related issue: #1340

@jatingajera31
Copy link
Author

@hanspagel I check #1340 issue but still I am getting problem.
I already add screenshot.

@hanspagel
Copy link
Member

Sure! Just wanted to cross link both issues.

We’re using vite-plugin-css-injected-by-js to inject the styles. I think we could use styleId and a custom injectCode function to make sure the style tag is only added once:

https://github.com/marco-prontera/vite-plugin-css-injected-by-js?tab=readme-ov-file#injectcode-function

@hanspagel hanspagel added the help wanted Extra attention is needed label May 14, 2024
@amritk
Copy link
Member

amritk commented May 14, 2024

@hanspagel @jatingajera31 there is a fix possible for this now with the new events. Just place the script tag outside of the page so it only gets loaded once, then you can trigger the custom event when you navigate to the page to reload the references

document.dispatchEvent(new Event('scalar:reload-references'))

but yes we should also only load the js and styles once

@amritk
Copy link
Member

amritk commented May 14, 2024

@jatingajera31 if you want to put up a minimal reproduction or just your code in general I can try and help

@jatingajera31
Copy link
Author

jatingajera31 commented May 15, 2024

@amritk here is the code
angular component file

`import { AfterViewInit, Component, OnDestroy, OnInit, Renderer2 } from '@angular/core';

@component({
selector: 'app-api-list',
standalone: true,
imports: [],
templateUrl: './api-list.component.html',
styleUrls: ['./api-list.component.scss'],
})
export class ApiListComponent implements OnInit, AfterViewInit, OnDestroy {
constructor() { }

ngOnInit(): void {
this.loadScript('https://cdn.jsdelivr.net/npm/@scalar/api-reference').then((res: any) => {
console.log('res:', res);
}).catch(error => {
console.error('Error loading script:', error);
});
}

ngOnDestroy(): void {
}

ngAfterViewInit(): void {
}

private loadScript(url: string): Promise {
return new Promise((resolve, reject) => {
const scriptElement = document.createElement('script');
scriptElement.src = url;
scriptElement.onload = () => resolve();
scriptElement.onerror = (error) => reject(error);
document.body.appendChild(scriptElement);
});
}
}`

html code file
<div id="api-reference" data-url="https://petstore3.swagger.io/api/v3/openapi.json" data-proxy-url="https://api.scalar.com/request-proxy"></div>

@amritk
Copy link
Member

amritk commented May 15, 2024

Alright I tested it and you are correct, even with the new events we still can't get it working in angular due to attaching the references to the body instead of the dom element inside the view. I think we should just make an angular integration at some point. I will open a ticket

@amritk
Copy link
Member

amritk commented May 15, 2024

You can track the work here

#1750

@jatingajera31
Copy link
Author

jatingajera31 commented May 16, 2024

@amritk Thank you for ticket.
I have fix that issue by removing (text/css) appending node from cdn link. I use this ( n.parentNode.removeChild(n))
image
Is this okay?

@amritk
Copy link
Member

amritk commented May 16, 2024

not sure, @hanspagel what do yo think

@hanspagel
Copy link
Member

Great you‘ve found a workaround! 🙌 As a general solution for our published version I’d still take the approach mentioned above:

#1724 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants