Skip to content

Commit

Permalink
Create S3 301 redirect objects for URLs without trailing slashes
Browse files Browse the repository at this point in the history
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
TheDahv committed Jul 27, 2018
1 parent 0535013 commit c03ac75
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
15 changes: 15 additions & 0 deletions gulpfile.js
@@ -1,6 +1,7 @@
const _ = require('underscore');
const awspublish = require('gulp-awspublish');
const del = require('del');
const es = require('event-stream');
const gulp = require('gulp');
const mkdirp = require('mkdirp');
const path = require('path');
Expand Down Expand Up @@ -114,6 +115,20 @@ gulp.task('create-sitemap', () => {
return transblogify.sitemap({ siteRoot: SITE_ROOT });
});

gulp.task('redirects', () => {
const {client} = awspublish.create(require('./awscredentials.json'));
return transblogify.redirects()
.pipe(es.map((Key, callback) => {
console.log(Key);
client.putObject({
ACL: 'public-read',
Key,
WebsiteRedirectLocation: `https://www.thedahv.com/${Key}/`
}, callback);
}))
.pipe(awspublish.reporter());
});

gulp.task('build', () => {
runSequence(
'clean',
Expand Down
20 changes: 13 additions & 7 deletions lib/transblogify/index.js
Expand Up @@ -8,22 +8,24 @@ const mkdirp = require('mkdirp');
const pages = require('./pages');
const path = require('path');
const posts = require('./posts');
const redirects = require('./redirects');
const reduce = require('stream-reduce');
const sitemap = require('./sitemap');
const w = require('when');

const transblogify = {};

const defaults = {
'categories': true,
'dataDir': 'data',
'defaultPostTemplate': 'post',
'outputDir': 'build',
'pagesDir': 'pages',
'postsDir': 'posts',
'postsPattern': '*.md',
'pagesDir': 'pages',
'outputDir': 'build',
'categories': true,
'redirectsDir': 'redirects',
'siteRoot': '',
'templatesDir': 'templates',
'defaultPostTemplate': 'post',
'dataDir': 'data',
'siteRoot': ''
};

function getTemplates(options) {
Expand Down Expand Up @@ -184,6 +186,10 @@ transblogify.pages = options => {
transblogify.sitemap = options => {
options = _.extend(defaults, options);
return sitemap(options);
}
};

transblogify.redirects = options => {
return redirects(_.extend(defaults, options));
};

module.exports = transblogify;
41 changes: 41 additions & 0 deletions lib/transblogify/redirects.js
@@ -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);
}))
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -30,7 +30,7 @@
"build": "gulp",
"clean": "gulp clean",
"dev": "gulp autoserve",
"publish": "gulp publish"
"publish": "gulp publish && gulp redirects"
},
"author": "",
"license": "BSD-2-Clause"
Expand Down

0 comments on commit c03ac75

Please sign in to comment.