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

feat(gatsby): SSR pages during development #27432

Merged
merged 149 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
149 commits
Select commit Hold shift + click to select a range
7a127f7
Prototype SSR wip
sidharthachatterjee Sep 2, 2020
3f920e3
Make it work
sidharthachatterjee Sep 3, 2020
0d614dd
Fix issue with running two instances of webpack
sidharthachatterjee Sep 3, 2020
4d073f5
Add a test suite for SSR
sidharthachatterjee Sep 8, 2020
88dc39e
Merge branch 'master' into feat/develop-ssr
sidharthachatterjee Sep 9, 2020
57a95ed
Linting
sidharthachatterjee Sep 9, 2020
19cf373
Run tests in CI
sidharthachatterjee Sep 9, 2020
655b685
Lint
sidharthachatterjee Sep 9, 2020
696487f
Merge branch 'master' into feat/develop-ssr
sidharthachatterjee Sep 9, 2020
da624c5
Merge remote-tracking branch 'origin/master' into k-develop-ssr
KyleAMathews Oct 12, 2020
89b671c
Show activity for HTML rendering + renable socket.io so server doesn'…
KyleAMathews Oct 13, 2020
a90ea0c
Add error page when templates don't render correctly
KyleAMathews Oct 13, 2020
d93d36d
Rebuild dev ssr bundle when source files change
KyleAMathews Oct 14, 2020
cfb1a3b
Fix some lint errors
KyleAMathews Oct 14, 2020
e4ab9c0
Fix building html
KyleAMathews Oct 14, 2020
ce714f3
use gatsby colors for syntax highlighting
KyleAMathews Oct 14, 2020
be7b2ae
Add test script to compare dev & prod output
KyleAMathews Oct 14, 2020
96c0f33
Add return types
KyleAMathews Oct 14, 2020
9d8bde2
Don't respond to dev server page loads until any sourcing/transformin…
KyleAMathews Oct 14, 2020
ac9642e
typings
KyleAMathews Oct 14, 2020
f8f00a3
fix types... maybe
KyleAMathews Oct 15, 2020
cbf16e0
maybe typescript happy
KyleAMathews Oct 15, 2020
1311c34
Add missing globals to tests & update snapshots
KyleAMathews Oct 15, 2020
4c86edb
moer merry type work
KyleAMathews Oct 15, 2020
f32c682
Remove outdated typography.js test
KyleAMathews Oct 15, 2020
0e0cefd
Start migrating route handler to spawned service
KyleAMathews Oct 15, 2020
494415f
back out moving dev html route into state machine
KyleAMathews Oct 16, 2020
ff59c2d
cleanups
KyleAMathews Oct 16, 2020
93e8fa1
more cleanups
KyleAMathews Oct 16, 2020
29a291e
yet moer cleanups
KyleAMathews Oct 16, 2020
8ab60c9
Add test for error parsing & codeframe creation
KyleAMathews Oct 16, 2020
68d2710
add return type
KyleAMathews Oct 16, 2020
2e05cb5
Feature flag functionality behind env variable
KyleAMathews Oct 16, 2020
616d856
Lighten how much the dev ssr html webpack instance is watching
KyleAMathews Oct 16, 2020
3442a00
cleanup
KyleAMathews Oct 16, 2020
424baaa
track usage while we're in experimental stage
KyleAMathews Oct 16, 2020
d4e4b2e
update snapshots
KyleAMathews Oct 16, 2020
cdea9a0
cleanup
KyleAMathews Oct 16, 2020
a10f070
Restore support for dev 404 page
KyleAMathews Oct 16, 2020
21cab9d
fix test
KyleAMathews Oct 16, 2020
c50327f
Catch reading errors
KyleAMathews Oct 19, 2020
6be8fe6
Make static queries work in dev-ssr
KyleAMathews Oct 19, 2020
f156049
Keep the renderer around
KyleAMathews Oct 19, 2020
b1eaa4a
Send 'loading' page if webpack is busy
KyleAMathews Oct 19, 2020
e11cc0a
Show more error so can debug CI
KyleAMathews Oct 19, 2020
00037e1
lazily build dev 404 page so it happens after webpack/queries are run
KyleAMathews Oct 19, 2020
5b8089a
just use /
KyleAMathews Oct 19, 2020
1cd80bf
Merge remote-tracking branch 'upstream/master' into k-develop-ssr
Oct 19, 2020
8031720
Add ansi-html as depdnency
KyleAMathews Oct 19, 2020
f44f285
Run test w/ experimental flag
KyleAMathews Oct 19, 2020
cabc58a
meaningless change to run tests again
KyleAMathews Oct 19, 2020
37bdaa0
use older version compatable with CI
KyleAMathews Oct 20, 2020
feab2f9
Update packages/gatsby/cache-dir/develop-static-entry.js
KyleAMathews Oct 20, 2020
8608b38
remove unnecessary changes
KyleAMathews Oct 20, 2020
9f16346
Consistently use imports
KyleAMathews Oct 20, 2020
2b3452b
fix
KyleAMathews Oct 20, 2020
e8da16c
Conditionally generate body str
KyleAMathews Oct 20, 2020
cc25d74
remove unneeded change
KyleAMathews Oct 20, 2020
27c18dd
Add find-page-by-path util from @pieh
pieh Oct 20, 2020
3059e56
make typescript happy
KyleAMathews Oct 20, 2020
78be606
Switch to use @pieh's page finder util
KyleAMathews Oct 20, 2020
d37c1c4
fix find-page-by-path tests
KyleAMathews Oct 21, 2020
a5b9b45
Enable dev ssr for tests
KyleAMathews Oct 21, 2020
9e23466
Add build:types again
sidharthachatterjee Oct 21, 2020
8a4f585
Do not await a flush
sidharthachatterjee Oct 21, 2020
05deee4
Merge remote-tracking branch 'upstream/master' into k-develop-ssr
Oct 21, 2020
bfccb50
Only delete the render-page.js module cache when it changes
KyleAMathews Oct 21, 2020
93d4a64
Try to reduce memory retention
KyleAMathews Oct 22, 2020
f1532e3
Fix recreating dev 404 page on every request + cache requires
KyleAMathews Oct 22, 2020
734f5b7
Add return
KyleAMathews Oct 22, 2020
36c33d7
this wasn't necessary
KyleAMathews Oct 22, 2020
b57cf79
Remove unused var
KyleAMathews Oct 22, 2020
bad3cda
fix return type
KyleAMathews Oct 22, 2020
257ec86
Share cache across develop/develop-html instances of webpack
KyleAMathews Oct 23, 2020
e33e843
This caused a lot of runtime tests to fail
KyleAMathews Oct 24, 2020
6482f03
Use the webpack hash
KyleAMathews Oct 24, 2020
26916f0
This didn't work
KyleAMathews Oct 24, 2020
b7fcd00
fix lint error
KyleAMathews Oct 24, 2020
37cf64c
Meaningless change to try tests again
KyleAMathews Oct 26, 2020
850231e
SSR pages in jest-worker so memory doesn't accumulate in main process
KyleAMathews Oct 26, 2020
2581da4
fix lint
KyleAMathews Oct 26, 2020
a4ffe3f
make typescript happy too
KyleAMathews Oct 26, 2020
98fbde1
fix test import
KyleAMathews Oct 26, 2020
07796c6
Merge remote-tracking branch 'upstream/master' into k-develop-ssr
Oct 27, 2020
a252870
Automatically fork the dev ssr renderer so it's ready to go when the …
KyleAMathews Oct 27, 2020
63765ed
Add structured logging on dev ssr failure
KyleAMathews Oct 27, 2020
3e9a3aa
Need require.resolve I think
KyleAMathews Oct 27, 2020
b284388
Add filepath + line/column to terminal error
KyleAMathews Oct 27, 2020
ebfa18d
try try again
KyleAMathews Oct 27, 2020
0df8483
Fixes hopefully
KyleAMathews Oct 27, 2020
1231fde
typescript 😱
KyleAMathews Oct 27, 2020
4994599
lint
KyleAMathews Oct 27, 2020
a0cb22c
Try tweaking jest settings
KyleAMathews Oct 27, 2020
822c482
Debuggin
KyleAMathews Oct 27, 2020
325cd4a
use default reporter
KyleAMathews Oct 27, 2020
421e993
explicitly init dev html worker pool so it doesn't start during tests
KyleAMathews Oct 27, 2020
85db9ec
restore original ci settings
KyleAMathews Oct 27, 2020
95a85b9
sup
KyleAMathews Oct 27, 2020
4c2b170
Update packages/gatsby-cli/src/structured-errors/error-map.ts
sidharthachatterjee Oct 27, 2020
45568e4
console.logs seem to break jest-worker on CI
KyleAMathews Oct 27, 2020
f2a862c
Increase pageLoadTimeout
KyleAMathews Oct 27, 2020
89da3ac
try taskTimeout
KyleAMathews Oct 27, 2020
e780c5d
This might be confusing cypress
KyleAMathews Oct 27, 2020
c7918b8
Don't re-spawn the worker process on every change as that's very expe…
KyleAMathews Oct 27, 2020
fd51701
cleanups
KyleAMathews Oct 27, 2020
2693163
Update packages/gatsby/cache-dir/develop-static-entry.js
KyleAMathews Oct 29, 2020
89b51b8
Cleanups suggested by @wardpeet
KyleAMathews Oct 29, 2020
55e44dc
fix lint
KyleAMathews Oct 29, 2020
850fe07
Lazily compile page components
KyleAMathews Oct 29, 2020
c5e740d
fix typescript
KyleAMathews Oct 29, 2020
c2a7191
mock /lazy-sync-requires
KyleAMathews Oct 29, 2020
dfeabe6
The lazy bundling created a race condition where two pages could be s…
KyleAMathews Oct 29, 2020
9d9af27
Add more pages to make sure we're going to hit the race condition
KyleAMathews Oct 29, 2020
e55886b
Check file directly that the page component has been added
KyleAMathews Oct 29, 2020
e63dd38
for some reason this lets log warnings from React not break jest-worker
KyleAMathews Oct 30, 2020
b13521f
fix test & comment
KyleAMathews Oct 30, 2020
d10ec64
We can't use the gatsby reporter inside a child as it uses process.se…
KyleAMathews Oct 30, 2020
4f159a8
Move writing lazyComponents to requires-writer & still use old develo…
KyleAMathews Oct 31, 2020
2ffb15d
update tests
KyleAMathews Oct 31, 2020
d58b886
Don't render body of pages w/ matchPath
KyleAMathews Oct 31, 2020
414430b
use core util joinPath so works on windows
KyleAMathews Oct 31, 2020
67c79b6
try again
KyleAMathews Oct 31, 2020
f70c177
try try again
KyleAMathews Oct 31, 2020
867bc42
remove mistakenly added file
KyleAMathews Oct 31, 2020
0462eb8
fix pnp test
KyleAMathews Oct 31, 2020
1d3604b
Merge branch 'master' into k-develop-ssr
KyleAMathews Nov 6, 2020
cb4c6f9
Move lazy bundling changes to https://github.com/gatsbyjs/gatsby/pull…
KyleAMathews Nov 9, 2020
acad5a8
Merge remote-tracking branch 'origin/master' into k-develop-ssr
KyleAMathews Nov 9, 2020
d0800ea
More cleanup
KyleAMathews Nov 9, 2020
305ae22
more cleanups
KyleAMathews Nov 9, 2020
681baa0
Test both old & new develop-static-entry
KyleAMathews Nov 9, 2020
1b9921e
fix lint
KyleAMathews Nov 9, 2020
80578d1
more lint fixes
KyleAMathews Nov 9, 2020
cab5b08
Use execa
KyleAMathews Nov 9, 2020
7ce4a3e
fix tests
KyleAMathews Nov 9, 2020
2740c42
Update names
KyleAMathews Nov 10, 2020
2c00092
Fail more gracefully when we source-maps don't work
KyleAMathews Nov 12, 2020
ce5f4ec
fix test & remove testing code
KyleAMathews Nov 12, 2020
a968156
Update snapshot
KyleAMathews Nov 12, 2020
33429f6
Merge branch 'master' into k-develop-ssr
KyleAMathews Nov 12, 2020
9cefce7
fix dependency check
KyleAMathews Nov 12, 2020
668cbba
remove unused import
KyleAMathews Nov 12, 2020
f26ae68
Update packages/gatsby/src/utils/dev-ssr/develop-html-route.ts
KyleAMathews Nov 16, 2020
cecc69a
revert unnecessary change
KyleAMathews Nov 16, 2020
671d73c
Merge branch 'master' into k-develop-ssr
KyleAMathews Nov 16, 2020
e557fca
restore more old behavior + move all new requires behind the flag
KyleAMathews Nov 16, 2020
67e0d0c
fix
KyleAMathews Nov 16, 2020
02dceb9
Merge branch 'master' into k-develop-ssr
KyleAMathews Nov 17, 2020
f91a18d
fix problem w/ merge w/ master
KyleAMathews Nov 17, 2020
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
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ jobs:
- e2e-test:
test_path: integration-tests/artifacts

integration_tests_ssr:
executor: node
steps:
- e2e-test:
test_path: integration-tests/ssr

e2e_tests_path-prefix:
<<: *e2e-executor
environment:
Expand Down Expand Up @@ -582,6 +588,8 @@ workflows:
<<: *e2e-test-workflow
- integration_tests_artifacts:
<<: *e2e-test-workflow
- integration_tests_ssr:
<<: *e2e-test-workflow
- integration_tests_gatsby_cli:
requires:
- bootstrap
Expand Down
21 changes: 21 additions & 0 deletions integration-tests/ssr/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 gatsbyjs

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.
1 change: 1 addition & 0 deletions integration-tests/ssr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## SSR test suite
20 changes: 20 additions & 0 deletions integration-tests/ssr/__tests__/__snapshots__/ssr.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SSR is run for a page when it is requested 1`] = `"<!DOCTYPE html><html><head><meta charSet=\\"utf-8\\"/><meta http-equiv=\\"x-ua-compatible\\" content=\\"ie=edge\\"/><meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1, shrink-to-fit=no\\"/><meta name=\\"note\\" content=\\"environment=development\\"/><script src=\\"/socket.io/socket.io.js\\"></script></head><body><div id=\\"___gatsby\\"><div style=\\"outline:none\\" tabindex=\\"-1\\" id=\\"gatsby-focus-wrapper\\"><div>Hello world</div></div><div id=\\"gatsby-announcer\\" style=\\"position:absolute;top:0;width:1px;height:1px;padding:0;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border:0\\" aria-live=\\"assertive\\" aria-atomic=\\"true\\"></div></div><script src=\\"/polyfill.js\\" nomodule=\\"\\"></script><script src=\\"/commons.js\\"></script></body></html>"`;

exports[`SSR it generates an error page correctly 1`] = `
"<title>Develop SSR Error</title><h1>Error<h1>
<h2>The page didn't SSR correctly</h2>
<ul>
<li><strong>URL path:</strong> <code>/bad-page/</code></li>
<li><strong>File path:</strong> <code>src/pages/bad-page.js</code></li>
</ul>
<h3>error message</h3>
<p><code>window is not defined</code></p><pre style=\\"background:#fdfaf6;padding:8px;\\"><span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"> <span style=\\"color:#527713;\\"> 2 | </span></span>
<span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"> <span style=\\"color:#527713;\\"> 3 | </span><span style=\\"color:#006500;\\">const</span> <span style=\\"color:#DB3A00;\\">Component</span> <span style=\\"color:#DB3A00;\\">=</span> () <span style=\\"color:#DB3A00;\\">=></span> {</span>
<span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"><span style=\\"color:#096fb3;\\"><span style=\\"font-weight:bold;\\">></span></span><span style=\\"color:#527713;\\"> 4 | </span> <span style=\\"color:#006500;\\">const</span> a <span style=\\"color:#DB3A00;\\">=</span> window<span style=\\"color:#DB3A00;\\">.</span>width</span>
<span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"> <span style=\\"color:#527713;\\"> | </span> <span style=\\"color:#096fb3;\\"><span style=\\"font-weight:bold;\\">^</span></span></span>
<span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"> <span style=\\"color:#527713;\\"> 5 | </span></span>
<span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"> <span style=\\"color:#527713;\\"> 6 | </span> <span style=\\"color:#006500;\\">return</span> <span style=\\"color:#DB3A00;\\"><</span><span style=\\"color:#DB3A00;\\">div</span><span style=\\"color:#DB3A00;\\">></span>hi<span style=\\"color:#DB3A00;\\"><</span><span style=\\"color:#DB3A00;\\">/</span><span style=\\"color:#DB3A00;\\">div</span><span style=\\"color:#DB3A00;\\">></span></span>
<span style=\\"font-weight:normal;opacity:1;color:#452475;background:#fdfaf6;\\"> <span style=\\"color:#527713;\\"> 7 | </span>}</span></pre>"
`;
9 changes: 9 additions & 0 deletions integration-tests/ssr/__tests__/fixtures/bad-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react"

const Component = () => {
const a = window.width

return <div>hi</div>
}

export default Component
55 changes: 55 additions & 0 deletions integration-tests/ssr/__tests__/ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fetch = require(`node-fetch`)
const execa = require(`execa`)
const fs = require(`fs-extra`)
const path = require(`path`)

describe(`SSR`, () => {
test(`is run for a page when it is requested`, async () => {
const html = await fetch(`http://localhost:8000/`).then(res => res.text())

expect(html).toMatchSnapshot()
})
test(`dev & build outputs match`, async () => {
const childProcess = await execa(`yarn`, [`test-output`])

expect(childProcess.code).toEqual(0)
})
test(`it generates an error page correctly`, async () => {
const src = path.join(__dirname, `/fixtures/bad-page.js`)
const dest = path.join(__dirname, `../src/pages/bad-page.js`)
fs.copySync(src, dest)

const pageUrl = `http://localhost:8000/bad-page/`
await new Promise(resolve => {
// Poll until the new page is bundled (so starts returning a non-404 status).
const testInterval = setInterval(() => {
fetch(pageUrl).then(res => {
if (res.status !== 404) {
clearInterval(testInterval)
resolve()
}
})
}, 1000)
})

const rawDevHtml = await fetch(
`http://localhost:8000/bad-page/`
).then(res => res.text())
expect(rawDevHtml).toMatchSnapshot()
fs.remove(dest)

// After the page is gone, it'll 404.
await new Promise(resolve => {
setTimeout(() => {
const testInterval = setInterval(() => {
fetch(pageUrl).then(res => {
if (res.status === 404) {
clearInterval(testInterval)
resolve()
}
})
}, 400)
}, 400)
})
})
})
10 changes: 10 additions & 0 deletions integration-tests/ssr/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
siteMetadata: {
title: `Hello world`,
author: `Sid Chatterjee`,
twitter: `chatsidhartha`,
github: `sidharthachatterjee`,
moreInfo: `Sid is amazing`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Lol

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems about right ;-)

},
plugins: [],
}
3 changes: 3 additions & 0 deletions integration-tests/ssr/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testPathIgnorePatterns: [`/node_modules/`, `__tests__/fixtures`, `.cache`],
}
39 changes: 39 additions & 0 deletions integration-tests/ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "ssr",
"description": "A simplified bare-bones starter for Gatsby.",
"version": "0.1.0",
"author": "Sid Chatterjee",
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"dependencies": {
"gatsby": "2.24.82-dev-1603131999086",
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"devDependencies": {
"cross-env": "^5.0.2",
"fs-extra": "^9.0.0",
"jest": "^24.0.0",
"jest-cli": "^24.0.0",
"jest-diff": "^24.0.0",
"npm-run-all": "4.1.5",
"start-server-and-test": "^1.11.3"
},
"license": "MIT",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-hello-world"
},
"scripts": {
"build": "gatsby build",
"clean": "gatsby clean",
"develop": "gatsby develop",
"serve": "gatsby serve",
"start-dev-server": "start-server-and-test develop http://localhost:8000 test:jest",
"test": "cross-env GATSBY_EXPERIMENTAL_DEV_SSR=true npm-run-all -s build start-dev-server",
"test-output": "node test-output.js",
"test:jest": "jest --config=jest.config.js --runInBand"
}
}
15 changes: 15 additions & 0 deletions integration-tests/ssr/src/pages/hi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { useStaticQuery, graphql } from "gatsby"

export default function Inline() {
const { site } = useStaticQuery(graphql`
{
site {
siteMetadata {
title
}
}
}
`)
return <div>hi1 {site.siteMetadata.title}</div>
}
15 changes: 15 additions & 0 deletions integration-tests/ssr/src/pages/hi2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { useStaticQuery, graphql } from "gatsby"

export default function Inline() {
const { site } = useStaticQuery(graphql`
{
site {
siteMetadata {
title
}
}
}
`)
return <div>hi2 {site.siteMetadata.title}</div>
}
15 changes: 15 additions & 0 deletions integration-tests/ssr/src/pages/hi3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { useStaticQuery, graphql } from "gatsby"

export default function Inline() {
const { site } = useStaticQuery(graphql`
{
site {
siteMetadata {
title
}
}
}
`)
return <div> hi3{site.siteMetadata.title}</div>
}
15 changes: 15 additions & 0 deletions integration-tests/ssr/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { useStaticQuery, graphql } from "gatsby"

export default function Inline() {
const { site } = useStaticQuery(graphql`
{
site {
siteMetadata {
title
}
}
}
`)
return <div>{site.siteMetadata.title}</div>
}
96 changes: 96 additions & 0 deletions integration-tests/ssr/test-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// To run the test script manually on a site (e.g. to test a plugin):
// - build the site first
// - start the develop server
// - run this script
;(async function () {
const { getPageHtmlFilePath } = require(`gatsby/dist/utils/page-html`)
const { join } = require(`path`)
const fs = require(`fs-extra`)
const fetch = require(`node-fetch`)
const diff = require(`jest-diff`)
const prettier = require(`prettier`)
const cheerio = require(`cheerio`)
const stripAnsi = require(`strip-ansi`)

const devSiteBasePath = `http://localhost:8000`

const comparePath = async path => {
const format = htmlStr => prettier.format(htmlStr, { parser: `html` })

const filterHtml = htmlStr => {
const $ = cheerio.load(htmlStr)
// There are many script tag differences
$(`script`).remove()
// Only added in production. Dev uses css-loader
$(`#gatsby-global-css`).remove()
// Only in prod
$(`link[rel="preload"]`).remove()
// Only in prod
$(`meta[name="generator"]`).remove()
// Only in dev
$(`meta[name="note"]`).remove()

return $.html()
}

const builtHtml = format(
filterHtml(
fs.readFileSync(
getPageHtmlFilePath(join(process.cwd(), `public`), path),
`utf-8`
)
)
)

const rawDevHtml = await fetch(`${devSiteBasePath}/${path}`).then(res =>
res.text()
)

const devHtml = format(filterHtml(rawDevHtml))
const diffResult = diff(devHtml, builtHtml, {
contextLines: 3,
expand: false,
})
if (
stripAnsi(diffResult) === `Compared values have no visual difference.`
) {
return true
} else {
console.log(`path "${path}" has differences between dev & prod`)
console.log(diffResult)
return false
}
}

const response = await fetch(`${devSiteBasePath}/__graphql`, {
method: `POST`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify({
query: `query MyQuery {
allSitePage {
nodes {
path
}
}
}
`,
}),
}).then(res => res.json()) // expecting a json response

const paths = response.data.allSitePage.nodes
.map(n => n.path)
.filter(p => p !== `/dev-404-page/`)

console.log(
`testing these paths for differences between dev & prod outputs`,
paths
)

const results = await Promise.all(paths.map(p => comparePath(p)))
// Test all true
if (results.every(r => r)) {
process.exit(0)
} else {
process.exit(1)
}
})()
2 changes: 2 additions & 0 deletions packages/babel-plugin-remove-graphql-queries/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export default function ({ types: t }): PluginObj {
JSXIdentifier(path2: NodePath<JSXIdentifier>): void {
if (
(process.env.NODE_ENV === `test` ||
state.opts.stage === `develop-html` ||
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
state.opts.stage === `build-html`) &&
path2.isJSXIdentifier({ name: `StaticQuery` }) &&
path2.referencesImport(`gatsby`, ``) &&
Expand Down Expand Up @@ -315,6 +316,7 @@ export default function ({ types: t }): PluginObj {
CallExpression(path2: NodePath<CallExpression>): void {
if (
(process.env.NODE_ENV === `test` ||
state.opts.stage === `develop-html` ||
state.opts.stage === `build-html`) &&
isUseStaticQuery(path2)
) {
Expand Down
14 changes: 14 additions & 0 deletions packages/gatsby-cli/src/structured-errors/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,20 @@ const errors = {
level: Level.ERROR,
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
},
"11614": {
text: ({
path,
filePath,
line,
column,
}): string => `The path "${path}" errored during SSR.

Edit its component ${filePath}${
line ? `:${line}:${column}` : ``
} to resolve the error.`,
level: Level.WARNING,
docsUrl: `https://gatsby.dev/debug-html`,
},
// Watchdog
"11701": {
text: (context): string =>
Expand Down
6 changes: 0 additions & 6 deletions packages/gatsby-plugin-typography/src/__tests__/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ describe(`onRenderBody`, () => {
])
})

it(`only invokes setHeadComponents if BUILD_STAGE is build-html`, () => {
const api = setup({}, `develop`)

expect(api.setHeadComponents).not.toHaveBeenCalled()
})

it(`does not add google font if omitGoogleFont is passed`, () => {
const api = setup({
omitGoogleFont: true,
Expand Down
Loading