Install the package
npm install @bufferapp/static-assets
To utilize the staticUrl
function, you'll need to first initialize the
static assets manager:
const { initStaticAssets } = require('@bufferapp/static-assets')
initStaticAssets()
.then(() => {
// Now that we have loaded the static assets we can serve requests
app.listen(8080, () => console.log('Server started!'))
})
Alternatively, you can call the initStaticAssetsSync
function to perform
a synchronous load of the static assets file which is blocking:
initStaticAssetsSync()
app.listen(8080, () => console.log('Server started!'))
Once the library is initialized, using the exported staticUrl
function:
const { staticUrl } = require('@bufferapp/static-assets')
app.get('/sweet', (req, res) => {
res.render('sweet-template', {
headerImage: staticUrl('img/sweet-header4.png'),
script: staticUrl('js/sweet-bundle.js'),
})
})
We have a few configuration options that can be passed to initStaticAssets
in
a single object argument (ex. initStaticAssets({ path: '/static' })
):
staticAssetVersionsFilename
- The name of the static asset manifest file.staticAssetDirLocal
- The relative path where static assets are served during local dev. Usually via express-static.staticAssetDirCDN
- This is the directory relative to your working directory where your static assets are saved. This is used to look up static files in the manifest (See manifest section below).The format of the static asset manifest should be the same as default JSON
format that buffer-static-upload
outputs.
This format is a key-value pair of:
"relative path to local file from working directory": "remote URL for that file"
Here's an example:
{
"public/css/style.css": "https://static.buffer.com/my-app/public/css/style.1234567890.css",
"public/img/brand.png": "https://static.buffer.com/my-app/public/img/brand.png"
}
MIT