Skip to content

Commit

Permalink
feat: Update partial hydration directive (#36868)
Browse files Browse the repository at this point in the history
* chore: Update client directive to "use client"

* Bump react-server-dom-webpack

* Adjust react-server-dom-webpack import paths

* Revert everything except directive change
  • Loading branch information
tyhopp committed Oct 25, 2022
1 parent db92b17 commit 7f1b57c
Show file tree
Hide file tree
Showing 18 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-cli/src/structured-errors/error-map.ts
Expand Up @@ -887,9 +887,9 @@ const errors = {
context?.path ? ` for path "${context.path}"` : ``
}
This can happen if interactive elements like "useEffect", "useState", "createContext" or event handlers are used in a component without declaring the "client export" directive at the top of the file.
This can happen if interactive elements like "useEffect", "useState", "createContext" or event handlers are used in a component without declaring the "use client" directive at the top of the file.
Consider adding "client export" to the top of your file if your component is interactive, otherwise refactor your component so it can be statically rendered with React Server Components (RSC).
Consider adding "use client" to the top of your file if your component is interactive, otherwise refactor your component so it can be statically rendered with React Server Components (RSC).
`),
level: Level.ERROR,
docsUrl: `https://gatsby.dev/partial-hydration-error`,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-link/package.json
Expand Up @@ -16,7 +16,7 @@
"sideEffects": false,
"scripts": {
"build": "npm-run-all --npm-path npm -s build:cjs build:esm",
"postbuild": "prepend-directive --files=dist/index.js,dist/index.modern.mjs --directive=\"client export\"",
"postbuild": "prepend-directive --files=dist/index.js,dist/index.modern.mjs --directive=\"use client\"",
"build:cjs": "microbundle -f cjs --jsx React.createElement --generateTypes false -i src/index-cjs.js -o dist/index.js",
"build:esm": "microbundle -f modern --jsx React.createElement --generateTypes false -o dist/index.mjs",
"watch": "npm-run-all --npm-path npm -p watch:cjs watch:esm",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-image/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "3.0.0-next.3",
"scripts": {
"build": "npm-run-all --npm-path npm -s clean -p build:*",
"postbuild": "prepend-directive --files=dist/gatsby-image.browser.js,dist/gatsby-image.browser.modern.js --directive=\"client export\"",
"postbuild": "prepend-directive --files=dist/gatsby-image.browser.js,dist/gatsby-image.browser.modern.js --directive=\"use client\"",
"build:gatsby-node": "tsc --jsx react --downlevelIteration true --skipLibCheck true --esModuleInterop true --outDir dist/ src/gatsby-node.ts src/babel-plugin-parse-static-images.ts src/resolver-utils.ts src/types.d.ts -d --declarationDir dist/src",
"build:gatsby-ssr": "microbundle -i src/gatsby-ssr.tsx -f cjs -o ./[name].js --no-pkg-main --jsx React.createElement --jsxFragment React.Fragment --no-compress --external=common-tags,react --no-sourcemap",
"build:server": "microbundle -f cjs,es --jsx React.createElement --jsxFragment React.Fragment --define SERVER=true",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-script/package.json
Expand Up @@ -16,7 +16,7 @@
"sideEffects": false,
"scripts": {
"build": "microbundle -f cjs,modern --jsx React.createElement && tsc --emitDeclarationOnly --declaration --declarationDir dist",
"postbuild": "prepend-directive --files=dist/index.js,dist/index.modern.mjs --directive=\"client export\"",
"postbuild": "prepend-directive --files=dist/index.js,dist/index.modern.mjs --directive=\"use client\"",
"watch": "microbundle watch -f cjs,modern --jsx React.createElement --no-compress",
"prepare": "cross-env NODE_ENV=production npm run clean && npm run build",
"clean": "del-cli dist/*"
Expand Down
@@ -1,4 +1,4 @@
"client export"
"use client"

exports.a = {
b: 1
Expand Down
@@ -1,4 +1,4 @@
"client export"
"use client"

// TODO: Handle, otherwise remove and document if we decide not to support or it's not supported by React's implementation

Expand Down
@@ -1,4 +1,4 @@
"client export"
"use client"

// TODO: Handle, otherwise remove and document if we decide not to support or it's not supported by React's implementation

Expand Down
@@ -1,4 +1,4 @@
"client export"
"use client"

export let a, b
export var c, d
Expand Down
@@ -1,3 +1,3 @@
"client export"
"use client"

export default class {}
@@ -1,3 +1,3 @@
"client export"
"use client"

export default class ClassName {}
@@ -1,4 +1,4 @@
"client export"
"use client"

const a = 1

Expand Down
@@ -1,3 +1,3 @@
"client export"
"use client"

export default function () {}
@@ -1,3 +1,3 @@
"client export"
"use client"

export default function functionName() {}
@@ -1,4 +1,4 @@
"client export"
"use client"

const a = 1, b = 2, c = 3

Expand Down
@@ -1,3 +1,3 @@
function noClientExport() {
return `No "client export" found in this file, and the function itself is not exported`
return `No "use client" found in this file, and the function itself is not exported`
}
@@ -1,3 +1,3 @@
export function noClientExport() {
return `No "client export" found in this file`
return `No "use client" found in this file`
}
Expand Up @@ -36,7 +36,7 @@ function createDefaultReference(
const partialHydrationReferenceLoader: LoaderDefinitionFunction<
Record<string, unknown>
> = async function partialHydrationReferenceLoader(content) {
if (!content.includes(`client export`)) {
if (!content.includes(`use client`)) {
return content
}

Expand All @@ -52,7 +52,7 @@ const partialHydrationReferenceLoader: LoaderDefinitionFunction<
ExpressionStatement(plainAcornNode: Node) {
const node = plainAcornNode as unknown as Directive

if (node.directive === `client export`) {
if (node.directive === `use client`) {
hasClientExportDirective = true
}
},
Expand Down
Expand Up @@ -232,7 +232,7 @@ export class PartialHydrationPlugin {
const hasClientExportDirective = ast.body.find(
statement =>
statement.type === `ExpressionStatement` &&
(statement as IDirective).directive === `client export`
(statement as IDirective).directive === `use client`
)

const module = parser.state.module
Expand Down

0 comments on commit 7f1b57c

Please sign in to comment.