Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create S3 301 redirect objects for URLs without trailing slashes
By default, S3 redirects a request for a directory (e.g., https://somesite.com/somepath) to the folder to serve its index.html file by default (e.g., https://somesite.com/somepath/). Its redirects are served with a 302 status code [1]. Best practice states that we want links to URLs without trailing slashes to redirect to the correct URL with a 301 [2]. This change generates the appropriate redirect objects on every publish configured with the correct redirect URL and redirect code [3]. 1: https://docs.aws.amazon.com/AmazonS3/latest/dev/IndexDocumentSupport.html 2: https://moz.com/learn/seo/redirection 3: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
- Loading branch information
Showing
4 changed files
with
70 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const es = require('event-stream'); | ||
const fs = require('fs'); | ||
const frontMatter = require('gulp-front-matter'); | ||
const gulp = require('gulp'); | ||
const mkdirp = require('mkdirp'); | ||
const path = require('path'); | ||
const posts = require('./posts'); | ||
const reduce = require('stream-reduce'); | ||
|
||
module.exports = options => { | ||
const { redirectsDir } = options || {}; | ||
const generateSlug = posts.generateSlug(); | ||
|
||
return gulp | ||
.src([ 'pages/**/*', 'posts/**/*' ], { read: true, } ) | ||
.pipe(es.map((file, callback) => { | ||
callback(null, file.isDirectory() ? undefined : file); | ||
})) | ||
.pipe(frontMatter({ property: 'frontMatter', remove: true })) | ||
.pipe(posts.generateSlug()) | ||
.pipe(es.map((file, callback) => { | ||
const { cwd, base, path, slug } = file; | ||
const type = base.replace(/\/$/, '').split('/').pop(); | ||
const page = slug | ||
? slug | ||
: path | ||
.replace(cwd, '') | ||
.split('/') | ||
.slice(2) | ||
.join('/') | ||
.replace(/(\.html|\.md)$/, ''); | ||
|
||
const entry = type === 'posts' ? | ||
[ 'blog', page ].join('/') | ||
: page === 'index' ? | ||
undefined | ||
: page | ||
|
||
callback(null, entry); | ||
})) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters