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

Make sure scripts and stylesheets are only loaded once #21

Closed
tauren opened this issue Sep 15, 2011 · 2 comments
Closed

Make sure scripts and stylesheets are only loaded once #21

tauren opened this issue Sep 15, 2011 · 2 comments

Comments

@tauren
Copy link

tauren commented Sep 15, 2011

Just thinking out loud, I haven't done any testing or research on this yet. Feel free to close this issue if you see no merit.

It would be nice to make sure that a script isn't loaded more than once. I'm thinking this could be done by checking the DOM to see if any of the existing script tags has the same src as the script that is about to be loaded.

I'm currently using feature detection to avoid loading modules multiple times. This works for things like jquery, underscore, backbone, etc. This means I'm wrapping code around each $script call. And feature detection wouldn't really work for scripts that don't add some property to an object within the window tree.

My use case is different than most who are building standard web pages. I'm creating widgets that can be installed anywhere. Many of the widgets I'm building have the same dependencies and load several additional libraries (jquery, underscore, backbone, etc.). So it would be very easy to load the same resources multiple times when multiple widgets are placed on a page.

Bottom line is that if more than one widget is placed on the same page, I'd like to make sure they both don't load the same resources. Perhaps feature detection is the best solution. But maybe it makes sense for the loader to simply check if the src is already in the DOM.

Also, if my pull request to add $script.styles is incorporated, it would be nice to have a way that stylesheets aren't loaded more than once as well. That is much harder to detect with feature detection.

@ded
Copy link
Owner

ded commented Sep 15, 2011

well, if you're using this utility to load all your js assets, then it already checks if you've previously loaded the same asset based on path. See here

eg:

// begin fetching bar.js
$script('/foo/bar.js', function () {

})

// bar.js is not loaded, but will receive callback
$script('/foo/bar.js', function () {

})

@tauren
Copy link
Author

tauren commented Sep 15, 2011

Sweet. I missed/forgot that I guess. I shouldn't go submitting issues until I've reviewed the source and docs again.

However, this indicates to me that already in the page and not loaded by $script would get loaded again. Is that correct?

Looks like I should update my $script.styles patch to include similar functionality.

@ded ded closed this as completed Sep 27, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants