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

requireJS javascript dependancies #1967

Closed
4 tasks
scd opened this issue Jul 22, 2015 · 4 comments
Closed
4 tasks

requireJS javascript dependancies #1967

scd opened this issue Jul 22, 2015 · 4 comments
Labels
5: Enhancement Build up on existing function that adds or improve functionality significantly Internal Performance

Comments

@scd
Copy link
Member

scd commented Jul 22, 2015

From @brendanheywood

So the topo template would say something like

% requireJS('topo.js');
  • If it gets required multiple times it gets ignored. The list of required js files files is retained in the order they were added.
  • the normal file path expansion into the versioned file would still work
  • Then the footer template can grab this list and render it in order. This would vastly simplify the way we manage the dependencies for each page and has the potential to streamline a lot of pages that don't need particular code.
  • I think it would be good if the function took a second argument, which is a javascript expression which if true means the script is should be loaded. For almost all this will default to just 'true'. This allows us to do stuff like:

% requireJS('topo.js', 'Modernizr.inlinesvg');

So the topos will only get loaded in a browser that can support them.

% requireJS('maps.js', 'window.matchMedia( "(min-width: 500px)" ).matches');

So that the full google maps widget will only load if the browser window is over a certain size(otherwise fall back to a static map).

This expression would need to be available to the footer template to render into the javascript to execute the test and then write the script tag if it passes

@brendanheywood
Copy link
Member

Thinking of making this a little more generic. A couple use cases:

  • css dependencies. Generally css is wacked into the header not the footer for speed, but there might be cases where we want to optionally include large chunks of css which load asynchronously in the footer. So exactly the same as above, but footer will pump out some different loading code.
  • inline svg, same again, a hidden inline svg file which then gets references by stuff elsewhere. Stuff like icons. Not 100% about this one, the best practice and rowser support keeps shifting, but would be nice to have it up our sleeves
  • inline data json. eg we want to access stuff like the meta data for a route, so render it as json into the page so we can later access via js. But only want to do this one per route, and the route could be references 10 times on a page. Again a bit of a weird case and I may not do it but nice to have it available.
  • potentially anything else that we can reference an de-duplicate in the source. It could also be used to simple mark something as rendered inline directly (rather than footer) so the next instance just omits it, instead of bundling them all into the footer.

So I'm thinking if we can just make this a little more generic by doing something like:

% require('js', 'topo.js', 'Modernizr.inlinesvg');

and then later in footer or whereever

% my @js = required('js') # note the 'd' on the end

The 3rd arg is arbitrary for require, and is just anything. It's only for the JS scope that is has a special meaning, so the full function signature could be:

% require(scope, key, value);

All are strings, value is optional. required(scope) returns an order list of a key => value pairs.

@scd
Copy link
Member Author

scd commented Jul 23, 2015

this is really good. I presume we are going to roll our own.

@brendanheywood
Copy link
Member

yeah this should only be a few lines of code

@brendanheywood brendanheywood added 5: Enhancement Build up on existing function that adds or improve functionality significantly Internal labels Apr 24, 2017
@brendanheywood brendanheywood added this to the Release 61 - xxx milestone Apr 15, 2018
@scd scd modified the milestones: Release 61 - GDPR prep release, Release 62 - xxx May 14, 2018
@brendanheywood brendanheywood modified the milestones: Release 62 - xxx, Release 6x? - New design May 16, 2018
@scd
Copy link
Member Author

scd commented Jan 10, 2021

done a while ago

@scd scd closed this as completed Jan 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5: Enhancement Build up on existing function that adds or improve functionality significantly Internal Performance
Projects
None yet
Development

No branches or pull requests

3 participants