Skip to content

Commit

Permalink
added local plugin to fix windows error (nodejs#2773)
Browse files Browse the repository at this point in the history
* added local plugin to fix windows error

* updated package.json for local plugin

* added readme and set the plugin package.json to private
  • Loading branch information
benhalverson authored Sep 20, 2022
1 parent 2cfa1b7 commit 328e0e2
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 76 deletions.
5 changes: 4 additions & 1 deletion gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const gatsbyConfig = {
'gatsby-plugin-sharp',
// This generates the redirects for the I18N redirects
// It also creates meta redirects for any usage of `createRedirect`
'gatsby-plugin-meta-redirect',
// 'gatsby-plugin-meta-redirect', replaced by local plugin because it breaks on windows
{
resolve: 'redirect',
},
...gatsbyFsMarkdownSources,
{
resolve: 'gatsby-plugin-canonical-urls',
Expand Down
74 changes: 0 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"gatsby-plugin-catch-links": "^4.22.0",
"gatsby-plugin-manifest": "^4.22.0",
"gatsby-plugin-mdx": "^3.20.0",
"gatsby-plugin-meta-redirect": "^1.1.1",
"gatsby-plugin-offline-next": "^5.2.3",
"gatsby-plugin-sass": "^5.22.0",
"gatsby-plugin-sharp": "^4.23.0",
Expand Down
21 changes: 21 additions & 0 deletions plugins/redirect/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Get Chalk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions plugins/redirect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This plugin is a replacement for the unmaintained `gatsby-plugin-meta-redirect`. The major change I made was removing the fs-extra module from it and using the built-in fs module instead.

The problem we were running into was the fs-extra module was throwing on an invalid character in our path for windows users.
39 changes: 39 additions & 0 deletions plugins/redirect/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
const path = require('path');
const fs = require('node:fs/promises');

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

async function writeRedirectsFile(redirects, folder, pathPrefix) {
if (!redirects.length) return;

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

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

try {
const data = getMetaRedirect(toPath);
await fs.writeFile(FILE_PATH, data);
} catch (_) {
// nothing to see here....
}
}
}

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);
};
7 changes: 7 additions & 0 deletions plugins/redirect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "gatsby-plugin-meta-redirect",
"version": "1.1.1",
"author": "Danny Wilson <dwilson@getchalk.com>",
"license": "MIT",
"private": true
}
18 changes: 18 additions & 0 deletions plugins/redirect/redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function redirect(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}'" />`;
};

0 comments on commit 328e0e2

Please sign in to comment.