Note that this guide is intended for Node.js users. If you are using a different language/server for your backend application check out the Generating HTML Snippets guide instead.
Bundle manifest files give you the information needed to know which <script> and <link> tags are needed for each bundle in your application. This guide will go through setting up a basic Express server, and using steal-bundle-manifest to attach the correct HTML needed, as well as provide Link; rel=preload headers (which will inform the browser to start loading the assets early).
Setup
We will be using the myhub app as a starting point. You can substitute your app if you want, otherwise clone the repo:
git clone git@github.com:stealjs/myhub.git
Next install the dependencies we need, there are just a few:
npm install steal-bundle-manifest express --save
Create server script
Create the following server.js file in the root of the project. For now it is just a skeleton, later we'll update it to include a template (using template strings) to send the HTML for each route.
In your devtools, if you look at the request for /puppies you'll see this:
Notice that there are Link headers for each of the assets on the page.
Using with a templating language
If you're using a Node server there's a good chance you are using a server templating language like EJS, Pug, or Handlebars. If that's the case, you can directly pass the scripts and styles arrays to your templating engine like so:
template({
scripts: scripts,
styles: styles
})
And then use them in your template directly. This example is in Handlebars/Mustache.
Bundle manifest files give you the information needed to know which
<script>
and<link>
tags are needed for each bundle in your application. This guide will go through setting up a basic Express server, and using steal-bundle-manifest to attach the correct HTML needed, as well as provideLink; rel=preload
headers (which will inform the browser to start loading the assets early).Setup
We will be using the myhub app as a starting point. You can substitute your app if you want, otherwise clone the repo:
Next install the dependencies we need, there are just a few:
Create server script
Create the following server.js file in the root of the project. For now it is just a skeleton, later we'll update it to include a template (using template strings) to send the HTML for each route.
Next, update the myhub.js script to use pathnames, rather than hashes, to determine which page to show:
And then update your server.js to look like:
Notice what this does:
manifest.for()
to get a bundle object.bundle.assets
to divide the assets between scripts and styles.bundle.toHTML()
to generate snippets of HTML, injected into the template literal.bundle.push()
to add the relevant Link headers.Now when you load the app on the puppies page your HTML looks like:
In your devtools, if you look at the request for
/puppies
you'll see this:Notice that there are Link headers for each of the assets on the page.
Using with a templating language
If you're using a Node server there's a good chance you are using a server templating language like EJS, Pug, or Handlebars. If that's the case, you can directly pass the
scripts
andstyles
arrays to your templating engine like so:And then use them in your template directly. This example is in Handlebars/Mustache.