Skip to content

Commit

Permalink
Completed readme
Browse files Browse the repository at this point in the history
  • Loading branch information
chungryan committed Aug 20, 2018
1 parent 5f54e5e commit 8ceb12b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
111 changes: 109 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,114 @@

Move all of your JS and CSS build files, as well as the static folder into a subdirectory of your choice.

## Our use case

Gatsby by default will generate all of the assets and put them directly at the root level:

```
public
│ index.html
│ component1.js
| component1.js.map
| component1.css
| component2.js
| compoennt2.js.map
| component3.css
└───path1
│ │ index.html
│ │ other1.html
│───path2
│ │ index.html
│ │ other2.html
|___static
| | data.json
| | image.jpg
```

However here at MadeComfy, we host our site on AWS Cloudfront/S3. One issue that we faced was that somehow, two different builds would have some JS/CSS files with the same file names even though their content are different.

That means during deployment on S3 and object invalidation on Cloudfront, someone that is currently browsing the site, would see the experience broken whilst moving onto other pages as the loaded JS would still have asset references of the previous build.

Hence our need to make sure that each build is kept intact on Cloudfront, except the HTML that are loaded on the browser at each hard reload. That way we make sure that our site has no down time at any point of time. We've configured our caching configs this way.

Using this plugin, our file struture is now as followed:

```
public
│ index.html
|___assets
| |___1534761288
│ | | component1.js
│ | | component1.js.map
│ | | component1.css
│ | | component2.js
│ | | compoennt2.js.map
│ | | component3.css
│ | |___static
│ | | | data.json
│ | | | image.jpg
└───path1
│ │ index.html
│ │ other1.html
│───path2
│ │ index.html
│ │ other2.html
```

Our new `assets` folder would contain assets of every build once on S3.

## Install

`npm install --save-dev gatsby-plugin-asset-path`
`yarn install -D gatsby-plugin-asset-path`
```
npm install --save-dev gatsby-plugin-asset-path
```
```
yarn install -D gatsby-plugin-asset-path
```

## How to use

```javascript
// Your gatsby-config.js
{
prefixPath: "custom_asset_folder",
plugins: [
{
resolve: "gatsby-plugin-asset-path"
}
]
}
```

In our use case above, we have `prefixPath` set as followed:
```javascript
{
prefixPath: `/assets/${Date.now().toString()}`
}
```

## Options

### removeMapFiles

Default: false

Stops Webpack from generating the .js.map files

```javascript
// Your gatsby-config.js
{
plugins: [
{
resolve: "gatsby-plugin-asset-path",
options: {
removeMapFiles: true
}
}
]
}
```

## Downside

Using `prefixPath` is not ideal as it is really used by Gatsby to generate the sites in the case it will be hosted in a subfolder. Using this plugin breaks that situation as it is assumed the site will be served from the domain root.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-plugin-asset-path",
"version": "0.0.4",
"version": "0.1.0",
"description": "Gatsby plugin that moves JS and CSS files and static folder to a custom folder",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 8ceb12b

Please sign in to comment.