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): Add defer to createPage #32783

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions packages/gatsby/src/bootstrap/requires-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ const getMatchPaths = (
const matchPathPages: Array<IMatchPathEntry> = []

pages.forEach((page: IGatsbyPage, index: number): void => {
if (page.matchPath) {
matchPathPages.push(createMatchPathEntry(page, index))
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
if (page.matchPath && page.mode === `SSG`) {
matchPathPages.push(createMatchPathEntry(page, index))
}
} else {
if (page.matchPath) {
matchPathPages.push(createMatchPathEntry(page, index))
}
}
})

Expand Down
21 changes: 17 additions & 4 deletions packages/gatsby/src/commands/build-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,27 @@ export function calcDirtyHtmlFiles(state: IGatsbyState): {
}

state.html.trackedHtmlFiles.forEach(function (htmlFile, path) {
if (htmlFile.isDeleted || !state.pages.has(path)) {
const page = state.pages.get(path)
if (htmlFile.isDeleted || !page) {
// FIXME: checking pages state here because pages are not persisted
// and because of that `isDeleted` might not be set ...
markActionForPage(path, `delete`)
} else if (htmlFile.dirty || state.html.unsafeBuiltinWasUsedInSSR) {
markActionForPage(path, `regenerate`)
} else {
markActionForPage(path, `reuse`)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
if (page.mode === `SSG`) {
if (htmlFile.dirty || state.html.unsafeBuiltinWasUsedInSSR) {
markActionForPage(path, `regenerate`)
} else {
markActionForPage(path, `reuse`)
}
}
} else {
if (htmlFile.dirty || state.html.unsafeBuiltinWasUsedInSSR) {
markActionForPage(path, `regenerate`)
} else {
markActionForPage(path, `reuse`)
}
}
}
})

Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ function createPageQueryJob(
}

const { path, componentPath, context } = page
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
const { mode } = page
if (mode !== `SSG`) {
return undefined
}
}
const { query } = component

return {
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/query/query-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ export async function queryRunner(
delete result.pageContext.componentPath
delete result.pageContext.context
delete result.pageContext.isCreatedByStatefulCreatePages
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
delete result.pageContext.mode
}
}

const resultJSON = JSON.stringify(result)
Expand Down
12 changes: 12 additions & 0 deletions packages/gatsby/src/redux/actions/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ type PageInput = {
component: string,
context?: Object,
ownerNodeId?: string,
defer?: boolean,
}

type PageMode = "SSG" | "DSR" | "SSR"

type Page = {
path: string,
matchPath: ?string,
Expand All @@ -108,6 +111,7 @@ type Page = {
componentChunkName: string,
updatedAt: number,
ownerNodeId?: string,
mode: PageMode,
}

type ActionOptions = {
Expand Down Expand Up @@ -407,6 +411,14 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
pluginCreatorId: plugin.id ?? ``,
}

if (_CFLAGS_.GATSBY_MAJOR === `4`) {
let pageMode: PageMode = `SSG`
if (page.defer) {
pageMode = `DSR`
}
internalPage.mode = pageMode
}

if (page.ownerNodeId) {
internalPage.ownerNodeId = page.ownerNodeId
}
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export enum ProgramStatus {
BOOTSTRAP_QUERY_RUNNING_FINISHED = `BOOTSTRAP_QUERY_RUNNING_FINISHED`,
}

export type PageMode = "SSG" | "DSR" | "SSR"

export interface IGatsbyPage {
internalComponentName: string
path: string
Expand All @@ -39,6 +41,7 @@ export interface IGatsbyPage {
pluginCreatorId: Identifier
componentPath: SystemPath
ownerNodeId: Identifier
mode: PageMode
}

export interface IGatsbyFunction {
Expand Down
5 changes: 3 additions & 2 deletions packages/gatsby/src/utils/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ export async function flush(): Promise<void> {
// This is why we need this check
if (page) {
if (
program?._?.[0] === `develop` &&
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
(program?._?.[0] === `develop` &&
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND) ||
(_CFLAGS_.GATSBY_MAJOR === `4` ? page.mode !== `SSG` : false)
) {
// check if already did run query for this page
// with query-on-demand we might have pending page-data write due to
Expand Down