forked from liferay/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes liferay#1180 - Add gatsby-plugin-meta-redirect to generate html…
… files for redirect
- Loading branch information
1 parent
eb76389
commit e8061eb
Showing
4 changed files
with
93 additions
and
21 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
50 changes: 50 additions & 0 deletions
50
clayui.com/plugins/gatsby-plugin-meta-redirect/gatsby-node.js
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,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
18
clayui.com/plugins/gatsby-plugin-meta-redirect/getMetaRedirect.js
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,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}'" />`; | ||
}; |
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,4 @@ | ||
{ | ||
"name": "gatsby-plugin-meta-redirect", | ||
"version": "0.0.1" | ||
} |