https://github.com/bufferapp/buffer-js-static-assets
https://github.com/bufferapp/buffer-js-static-assets
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bufferapp/buffer-js-static-assets
- Owner: bufferapp
- License: mit
- Created: 2018-12-18T11:20:48.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-22T11:27:53.000Z (about 7 years ago)
- Last Synced: 2025-02-12T10:13:18.435Z (12 months ago)
- Language: JavaScript
- Size: 71.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# buffer-js-static-assets
[](https://travis-ci.org/bufferapp/buffer-js-static-assets)
## Installation
Install the package
```
npm install @bufferapp/static-assets
```
## Usage
To utilize the `staticUrl` function, you'll need to first initialize the
static assets manager:
```js
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:
```js
initStaticAssetsSync()
app.listen(8080, () => console.log('Server started!'))
```
Once the library is initialized, using the exported `staticUrl` function:
```js
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'),
})
})
```
### Options
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).
### Static Asset Manifest
The format of the static asset manifest should be the same as default JSON
format that [`buffer-static-upload`](https://github.com/bufferapp/buffer-static-upload)
outputs.
This format is a key-value pair of:
```json
"relative path to local file from working directory": "remote URL for that file"
```
Here's an example:
```json
{
"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"
}
```
## License
MIT