Skip to content

Commit 883d184

Browse files
GatsbyJS BotKyleAMathews
andauthored
feature(gatsby): Extract non-css-in-js css and add add to <head> when SSRing in dev (#28471) (#28856)
* feature(gatsby): Pause dev-ssr watching between page loads to avoid slowing down regular develop-js HMR * update snapshot * Don't double-resolve + add activity for building the SSR bundle * Add timeout for tests to ensure that dev server has time to bundle SSR + remove activity timers as not helpful * feature(gatsby): Extract and add CSS when SSRing in dev * Remove commented out code * get tests passing * WIP * Got hot-reloading working w/ mini-css-extract-plugin * remove mistakenly added file * remove change to yarn.lock * revert other mistakenly added files * Add an async module to test against * fix async module * Add postcss/tailwind * write webpack config for easy comparisons * that was a lot easier than I thought — just set hmr:true for non-production sites * cleanups * Don't need this since we're using <link> not <style> * pass in port * remove dev css from test comparisons * Update snapshots + add tailwind * cleanups * remove discarded changes * Move changes behind flag * Undo unnecesary changes * Update tests for signature change * Move more code behind the flag * dynamically set absolute URL for css files so works wherever it's hosted * start relative than make absolute * Remove now unused port * Remove changes from #28394 * use @pieh's suggested refactor in https://github.com/gatsbyjs/gatsby/pull/28471/files\#r546803732 * pass naming options for extractText in via options * Update packages/gatsby/src/utils/webpack.config.js Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com> * Update snapshot * Stop Jest from chocking on import of css * turned out we didn't need this * test(ssr): ignore src/test file (those are not tests) * test(ssr): update snapshot after removing inline script modyfing href Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com> (cherry picked from commit 121ccbf) Co-authored-by: Kyle Mathews <mathews.kyle@gmail.com>
1 parent b648728 commit 883d184

File tree

20 files changed

+180
-20
lines changed

20 files changed

+180
-20
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

integration-tests/ssr/__tests__/__snapshots__/ssr.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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>"`;
3+
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\\"/><link data-identity=\\"gatsby-dev-css\\" rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"/commons.css\\"/><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><h1 class=\\"hi\\">Hello world</h1></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>"`;
44
55
exports[`SSR it generates an error page correctly 1`] = `
66
"<head>

integration-tests/ssr/__tests__/ssr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ describe(`SSR`, () => {
5353
}, 400)
5454
}, 400)
5555
})
56-
})
56+
}, 15000)
5757
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./sample.css"

integration-tests/ssr/gatsby-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ module.exports = {
66
github: `sidharthachatterjee`,
77
moreInfo: `Sid is amazing`,
88
},
9-
plugins: [],
9+
plugins: ["gatsby-plugin-postcss"],
1010
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
module.exports = {
2-
testPathIgnorePatterns: [`/node_modules/`, `__tests__/fixtures`, `.cache`],
2+
testPathIgnorePatterns: [
3+
`/node_modules/`,
4+
`__tests__/fixtures`,
5+
`.cache`,
6+
`src/test`,
7+
],
8+
transform: {
9+
"^.+\\.[jt]sx?$": `<rootDir>../../jest-transformer.js`,
10+
},
11+
moduleNameMapper: {
12+
"\\.(css)$": `<rootDir>/__mocks__/styleMock.js`,
13+
},
314
}

integration-tests/ssr/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
},
99
"dependencies": {
1010
"gatsby": "^2.27.0",
11+
"gatsby-plugin-postcss": "^3.3.0",
1112
"react": "^16.12.0",
12-
"react-dom": "^16.12.0"
13+
"react-dom": "^16.12.0",
14+
"tailwindcss": "1"
1315
},
1416
"devDependencies": {
1517
"cross-env": "^5.0.2",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: [require("tailwindcss"), require("autoprefixer")],
3+
}

integration-tests/ssr/sample.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
background: tomato;
3+
color: black;
4+
font-style: italic;
5+
font-weight: 400;
6+
}

integration-tests/ssr/src/pages/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react"
22
import { useStaticQuery, graphql } from "gatsby"
3+
const lazyImport = import(`../test`)
34

45
export default function Inline() {
56
const { site } = useStaticQuery(graphql`
@@ -11,5 +12,9 @@ export default function Inline() {
1112
}
1213
}
1314
`)
14-
return <div>{site.siteMetadata.title}</div>
15+
return (
16+
<div>
17+
<h1 className="hi">{site.siteMetadata.title}</h1>
18+
</div>
19+
)
1520
}

0 commit comments

Comments
 (0)