Skip to content

Commit

Permalink
Fixes liferay#1180 - Add gatsby-plugin-meta-redirect to generate html…
Browse files Browse the repository at this point in the history
… files for redirect
  • Loading branch information
matuzalemsteles committed Sep 28, 2018
1 parent eb76389 commit e8061eb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 21 deletions.
42 changes: 21 additions & 21 deletions clayui.com/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ const clay = require('clay-css');
const path = require('path');

module.exports = {
plugins: [
'gatsby-plugin-react-next',
'gatsby-transformer-try-examples',
{
resolve: 'gatsby-plugin-sass',
options: {
precision: 8,
includePaths: clay
.includePaths
.concat(
path.join(
clay.includePaths[0],
'node_modules'
)
)
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
name: "packages",
plugins: [
'gatsby-plugin-meta-redirect',
'gatsby-transformer-try-examples',
{
resolve: 'gatsby-plugin-sass',
options: {
precision: 8,
includePaths: clay
.includePaths
.concat(
path.join(
clay.includePaths[0],
'node_modules'
)
),
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
name: 'packages',
path: `${__dirname}/content`,
},
},
Expand Down
50 changes: 50 additions & 0 deletions clayui.com/plugins/gatsby-plugin-meta-redirect/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const path = require('path');
const {exists, writeFile, ensureDir} = require('fs-extra');

const getMetaRedirect = require('./getMetaRedirect');

// Adapted from https://github.com/getchalk/gatsby-plugin-meta-redirect
async function writeRedirectsFile(redirects, folder, pathPrefix) {
if (!redirects.length) return;

for (const redirect of redirects) {
const {fromPath, toPath} = redirect;

if (fromPath.endsWith('index.html')) continue;

const FILE_PATH = path.join(
folder,
fromPath.replace(pathPrefix, ''),
'index.html'
);

let fileExists;

try {
fileExists = await exists(FILE_PATH);
} catch (err) {}

if (!fileExists) {
try {
await ensureDir(path.dirname(FILE_PATH));
} catch (err) {
// ignore if the directory already exists;
}

const data = getMetaRedirect(toPath);
await writeFile(FILE_PATH, data);
}
}
}

exports.onPostBuild = ({store}) => {
const {redirects, program, config} = store.getState();

let pathPrefix = '';
if (program.prefixPaths) {
pathPrefix = config.pathPrefix;
}

const folder = path.join(program.directory, 'public');
return writeRedirectsFile(redirects, folder, pathPrefix);
};
18 changes: 18 additions & 0 deletions clayui.com/plugins/gatsby-plugin-meta-redirect/getMetaRedirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function getMetaRedirect(toPath) {
let url = toPath.trim();

const hasProtocol = url.includes('://');
if (!hasProtocol) {
const hasLeadingSlash = url.startsWith('/');
if (!hasLeadingSlash) {
url = `/${url}`;
}

const resemblesFile = url.includes('.');
if (!resemblesFile) {
url = `${url}/`.replace(/\/\/+/g, '/');
}
}

return `<meta http-equiv="refresh" content="0; URL='${url}'" />`;
};
4 changes: 4 additions & 0 deletions clayui.com/plugins/gatsby-plugin-meta-redirect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "gatsby-plugin-meta-redirect",
"version": "0.0.1"
}

0 comments on commit e8061eb

Please sign in to comment.