Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): Fix various small DEV_SSR bugs exposed in development_runtime tests #29720

Merged
merged 13 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const COUNT_ID = `count`

describe(`hooks`, () => {
beforeEach(() => {
cy.visit(`/hooks`).waitForRouteChange()
cy.visit(`/hooks`, { failOnStatusCode: false }).waitForRouteChange()
KyleAMathews marked this conversation as resolved.
Show resolved Hide resolved
})

it(`displays initial state`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ function pageTitleAndDataAssertion(config) {
function runTests(config) {
preTestSetup(config)

cy.visit(`/query-data-caches/${config.slug}/page-A/`).waitForRouteChange()
cy.visit(`/query-data-caches/${config.slug}/page-A/`, {
failOnStatusCode: false,
}).waitForRouteChange()

setupForAssertingNotReloading()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ describe(`navigation`, () => {

describe(`non-existent route`, () => {
beforeEach(() => {
cy.getTestElement(`broken-link`).click().waitForRouteChange()
cy.getTestElement(`broken-link`)
.click({ failOnStatusCode: false })
.waitForRouteChange()
})

it(`displays 404 page on broken link`, () => {
Expand Down Expand Up @@ -134,7 +136,7 @@ describe(`navigation`, () => {
})

it(`should show 404 page when url with unicode characters point to a non-existent page route when navigating on client`, () => {
cy.visit(`/`).waitForRouteChange()
cy.visit(`/`, { failOnStatusCode: false }).waitForRouteChange()
cy.window()
.then(win => win.___navigate(`/안녕404/`))
.waitForRouteChange()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Cypress.on(`window:before:load`, win => {

const runTests = () => {
it(`should redirect page to index page when there is no such page`, () => {
cy.visit(`/redirect-without-page`).waitForRouteChange()
cy.visit(`/redirect-without-page`, {
failOnStatusCode: false,
}).waitForRouteChange()

cy.location(`pathname`).should(`equal`, `/`)
cy.then(() => {
Expand Down Expand Up @@ -42,7 +44,7 @@ const runTests = () => {
})

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

cy.location(`pathname`).should(`equal`, `/pt/redirect-me/`)
cy.then(() => {
Expand All @@ -65,7 +67,9 @@ describe(`redirect`, () => {

// this is sanity check for this group
it(`make sure 404 is present`, () => {
cy.visit(`/______not_existing_page`).waitForRouteChange()
cy.visit(`/______not_existing_page`, {
failOnStatusCode: false,
}).waitForRouteChange()
cy.findByText("Preview custom 404 page").click()
cy.findByText("A custom 404 page wasn't detected", {
exact: false,
Expand Down Expand Up @@ -100,7 +104,9 @@ describe(`redirect`, () => {
})

it(`make sure 404 is NOT present`, () => {
cy.visit(`/______not_existing_page`).waitForRouteChange()
cy.visit(`/______not_existing_page`, {
failOnStatusCode: false,
}).waitForRouteChange()
cy.findByText("Preview custom 404 page").click()
cy.findByText("A custom 404 page wasn't detected", {
exact: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe(`page not found`, () => {
beforeEach(() => {
cy.visit(`/__404__`)
cy.visit(`/__404__`, { failOnStatusCode: false })
})
it(`should display message `, () => {
cy.get(`h1`).invoke(`text`).should(`eq`, `Gatsby.js development 404 page`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
undefined,
// Client only pages have any empty body so we just do a normal
// render to avoid React complaining about hydration mis-matches.
document.getElementById(`___gatsby`).children.length === 0
document.getElementById(`gatsby-focus-wrapper`).children.length === 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

___gatsby always has a child so wasn't actually testing if there was SSRed content or not so client-only pages would show a hydration warning.

? ReactDOM.render
: ReactDOM.hydrate
)[0]
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/utils/dev-ssr/develop-html-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export const route = ({ app, program, store }): any =>
app.get(`*`, async (req, res, next) => {
trackFeatureIsUsed(`GATSBY_EXPERIMENTAL_DEV_SSR`)

const pathObj = findPageByPath(store.getState(), req.path)
const pathObj = findPageByPath(store.getState(), decodeURI(req.path))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle paths with unicode characters


if (!pathObj) {
return next()
}

await appendPreloadHeaders(req.path, res)
await appendPreloadHeaders(pathObj.path, res)

const htmlActivity = report.phantomActivity(`building HTML for path`, {})
htmlActivity.start()
Expand Down Expand Up @@ -152,7 +152,7 @@ export const route = ({ app, program, store }): any =>
node.js, it errored.
</p>
<ul>
<li><strong>URL path:</strong> <code>${req.path}</code></li>
<li><strong>URL path:</strong> <code>${pathObj.path}</code></li>
<li><strong>File path:</strong> <code>${error.filename}</code></li>
</ul>
<h3>error</h3>
Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby/src/utils/dev-ssr/render-dev-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ export const restartWorker = (htmlComponentRendererPath): void => {

const searchFileForString = (substring, filePath): Promise<boolean> =>
new Promise(resolve => {
const escapedSubString = substring.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle paths with special characters


// See if the chunk is in the newComponents array (not the notVisited).
const chunkRegex = RegExp(`exports.ssrComponents.*${substring}.*}`, `gs`)
const chunkRegex = RegExp(
`exports.ssrComponents.*${escapedSubString}.*}`,
`gs`
)
const stream = fs.createReadStream(filePath)
let found = false
stream.on(`data`, function (d) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/develop-preload-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function appendPreloadHeaders(
`Link`,
`</${path.join(
`page-data`,
fixedPagePath(pagePath),
encodeURI(fixedPagePath(pagePath)),
`page-data.json`
)}>; rel=preload; as=fetch ; crossorigin`
)
Expand Down
10 changes: 1 addition & 9 deletions packages/gatsby/src/utils/flags.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import _ from "lodash"
import semver from "semver"

import sampleSiteForExperiment from "./sample-site-for-experiment"

// Does this experiment run for only builds
type executingCommand = "build" | "develop" | "all"

Expand Down Expand Up @@ -94,13 +92,7 @@ const activeFlags: Array<IFlag> = [
experimental: false,
description: `Server Side Render (SSR) pages on full reloads during develop. Helps you detect SSR bugs and fix them without needing to do full builds.`,
umbrellaIssue: `https://gatsby.dev/dev-ssr-feedback`,
testFitness: (): fitnessEnum => {
if (sampleSiteForExperiment(`DEV_SSR`, 20)) {
return `OPT_IN`
} else {
return true
}
},
testFitness: (): fitnessEnum => `LOCKED_IN`,
},
{
name: `QUERY_ON_DEMAND`,
Expand Down