-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): don't show error for proper redirects (#19789)
* Created isPageExist method in loader.js, added e2e tests for redirect feature * make function dev-only and simplify logic * e2e: move to navigation directory and spy on console.error to validate * remove comment from copy pasting * rename method to mark this is not public API and shouldn't be used (we do pass loader to browser APIs) * remove debug code * more removals * test case of 404 not presetnt as well * apparently some changes we did during resilient resource loading made this work properly regardless if 404 is present * fixup - remove commented out export Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
- Loading branch information
Showing
6 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
e2e-tests/development-runtime/cypress/integration/navigation/redirect.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,98 @@ | ||
let spy | ||
Cypress.on(`window:before:load`, win => { | ||
spy = cy.spy(win.console, `error`).as(`spyWinConsoleError`) | ||
}) | ||
|
||
const runTests = () => { | ||
it(`should redirect page to index page when there is no such page`, () => { | ||
cy.visit(`/redirect-without-page`).waitForRouteChange() | ||
|
||
cy.location(`pathname`).should(`equal`, `/`) | ||
cy.then(() => { | ||
const calls = spy.getCalls() | ||
|
||
const callsAboutRedirectMatchingPage = calls.filter(call => { | ||
return call.args[0].includes( | ||
"matches both a page and a redirect; this is probably not intentional." | ||
) | ||
}) | ||
|
||
expect(callsAboutRedirectMatchingPage.length).to.equal(0) | ||
}) | ||
}) | ||
|
||
it(`should redirect page to index page even there is a such page`, () => { | ||
cy.visit(`/redirect`).waitForRouteChange() | ||
|
||
cy.location(`pathname`).should(`equal`, `/`) | ||
cy.then(() => { | ||
const calls = spy.getCalls() | ||
|
||
const callsAboutRedirectMatchingPage = calls.filter(call => { | ||
return call.args[0].includes( | ||
"matches both a page and a redirect; this is probably not intentional." | ||
) | ||
}) | ||
|
||
expect(callsAboutRedirectMatchingPage.length).not.to.equal(0) | ||
expect(spy).to.be.calledWith( | ||
`The route "/redirect" matches both a page and a redirect; this is probably not intentional.` | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
describe(`redirect`, () => { | ||
describe("404 is present", () => { | ||
before(() => { | ||
cy.task(`restoreAllBlockedResources`) | ||
}) | ||
|
||
// this is sanity check for this group | ||
it(`make sure 404 is present`, () => { | ||
cy.visit(`/______not_existing_page`).waitForRouteChange() | ||
cy.queryByText("Preview custom 404 page").click() | ||
cy.queryByText("A custom 404 page wasn't detected", { | ||
exact: false, | ||
}).should(`not.exist`) | ||
cy.queryByText( | ||
"You just hit a route that does not exist... the sadness." | ||
).should(`exist`) | ||
}) | ||
|
||
runTests() | ||
}) | ||
|
||
describe("no 404", () => { | ||
before(() => { | ||
cy.task(`restoreAllBlockedResources`) | ||
|
||
cy.task(`blockAssetsForPage`, { | ||
pagePath: `/404`, | ||
filter: `page-data`, | ||
}) | ||
cy.task(`blockAssetsForPage`, { | ||
pagePath: `/404.html`, | ||
filter: `page-data`, | ||
}) | ||
}) | ||
|
||
after(() => { | ||
cy.task(`restoreAllBlockedResources`) | ||
}) | ||
|
||
it(`make sure 404 is NOT present`, () => { | ||
cy.visit(`/______not_existing_page`).waitForRouteChange() | ||
cy.queryByText("Preview custom 404 page").click() | ||
cy.queryByText("A custom 404 page wasn't detected", { | ||
exact: false, | ||
}).should(`exist`) | ||
cy.queryByText( | ||
"You just hit a route that does not exist... the sadness.", | ||
{ exact: false } | ||
).should(`not.exist`) | ||
}) | ||
|
||
runTests() | ||
}) | ||
}) |
66 changes: 66 additions & 0 deletions
66
e2e-tests/development-runtime/cypress/plugins/block-resources.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,66 @@ | ||
// this is partial copy of e2e-tests/production-runtime/cypress/plugins/block-resources.js | ||
// partial because there is no asset blocking possibly in develop (we have single, virtual development bundle) | ||
|
||
const fs = require(`fs-extra`) | ||
const path = require(`path`) | ||
const glob = require(`glob`) | ||
|
||
const publicDir = path.join(__dirname, `..`, `..`, `public`) | ||
|
||
const moveAsset = (from, to) => { | ||
const fromExists = fs.existsSync(from) | ||
const toExists = fs.existsSync(to) | ||
|
||
if (fromExists && !toExists) { | ||
fs.moveSync(from, to, { | ||
overwrite: true, | ||
}) | ||
} | ||
} | ||
|
||
const restorePageData = hiddenPath => { | ||
if (path.basename(hiddenPath).charAt(0) !== `_`) { | ||
throw new Error(`hiddenPath should have _ prefix`) | ||
} | ||
const restoredPath = path.join( | ||
path.dirname(hiddenPath), | ||
path.basename(hiddenPath).slice(1) | ||
) | ||
moveAsset(hiddenPath, restoredPath) | ||
} | ||
|
||
const getPageDataPath = pagePath => { | ||
const fixedPagePath = pagePath === `/` ? `index` : pagePath | ||
return path.join(publicDir, `page-data`, fixedPagePath, `page-data.json`) | ||
} | ||
|
||
const getHiddenPageDataPath = pagePath => { | ||
const fixedPagePath = pagePath === `/` ? `index` : pagePath | ||
return path.join(publicDir, `page-data`, fixedPagePath, `_page-data.json`) | ||
} | ||
|
||
const blockPageData = pagePath => | ||
moveAsset(getPageDataPath(pagePath), getHiddenPageDataPath(pagePath)) | ||
|
||
const blockAssetsForPage = ({ pagePath, filter }) => { | ||
if (filter === `all` || filter === `page-data`) { | ||
blockPageData(pagePath) | ||
} | ||
|
||
console.log(`Blocked assets for path "${pagePath}" [${filter}]`) | ||
return null | ||
} | ||
|
||
const restore = () => { | ||
const globPattern = path.join(publicDir, `/page-data/**`, `_page-data.json`) | ||
const hiddenPageDatas = glob.sync(globPattern) | ||
hiddenPageDatas.forEach(restorePageData) | ||
|
||
console.log(`Restored resources`) | ||
return null | ||
} | ||
|
||
module.exports = { | ||
restoreAllBlockedResources: restore, | ||
blockAssetsForPage, | ||
} |
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,13 @@ | ||
import React from "react" | ||
|
||
import Layout from "../components/layout" | ||
import SEO from "../components/seo" | ||
|
||
const Redirect = () => ( | ||
<Layout> | ||
<SEO title="Redirect" /> | ||
<p>Redirecting...</p> | ||
</Layout> | ||
) | ||
|
||
export default Redirect |
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