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