Skip to content

Commit

Permalink
fix(gatsby): Recognise null pages as not found (#27003)
Browse files Browse the repository at this point in the history
* Show null page as not found

* Don't register null paths

* Add test for redirect
  • Loading branch information
ascorbic committed Sep 23, 2020
1 parent cb5c3d1 commit 18944ce
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ const runTests = () => {
)
})
})

it(`should redirect to a dynamically-created replacement page`, () => {
cy.visit(`/redirect-me/`).waitForRouteChange()

cy.location(`pathname`).should(`equal`, `/pt/redirect-me/`)
cy.then(() => {
expect(spy).not.to.be.calledWith(
`The route "/redirect" matches both a page and a redirect; this is probably not intentional.`
)

cy.findByText("This should be at /pt/redirect-me/", {
exact: false,
}).should(`exist`)
})
})
}

describe(`redirect`, () => {
Expand Down
26 changes: 25 additions & 1 deletion e2e-tests/development-runtime/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,34 @@ exports.createPages = async function createPages({
}

exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions
const { createPage, createRedirect, deletePage } = actions

if (page.path.match(/^\/client-only-paths/)) {
page.matchPath = `/client-only-paths/*`
createPage(page)
}

if (page.path === `/redirect-me/`) {
const toPath = `/pt${page.path}`

deletePage(page)

createRedirect({
fromPath: page.path,
toPath,
isPermanent: false,
redirectInBrowser: true,
Language: `pt`,
statusCode: 301,
})

createPage({
...page,
path: toPath,
context: {
...page.context,
lang: `pt`,
},
})
}
}
13 changes: 13 additions & 0 deletions e2e-tests/development-runtime/src/pages/redirect-me.js
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>This should be at /pt/redirect-me/</p>
</Layout>
)

export default Redirect
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class BaseLoader {
isPageNotFound(rawPath) {
const pagePath = findPath(rawPath)
const page = this.pageDb.get(pagePath)
return page && page.notFound === true
return !page || page.notFound
}

loadAppData(retries = 0) {
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby/src/utils/websocket-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ export class WebsocketManager {

socket.on(`registerPath`, (path: string): void => {
socket.join(getRoomNameFromPath(path))
activePath = path
this.activePaths.add(path)
if (path) {
activePath = path
this.activePaths.add(path)
}
})

socket.on(`disconnect`, (): void => {
Expand Down

0 comments on commit 18944ce

Please sign in to comment.