Skip to content

Commit

Permalink
refactor(websocket): unify page-data emitting (#25190)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Jun 22, 2020
1 parent e288131 commit 38eed44
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/query-result-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class PageQueryStore extends React.Component {
return <div />
}

return <PageRenderer {...this.props} {...data} />
return <PageRenderer {...this.props} {...data.result} />
}
}

Expand Down
10 changes: 3 additions & 7 deletions packages/gatsby/src/utils/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface IPageData {
path: IGatsbyPage["path"]
}

interface IPageDataWithQueryResult extends IPageData {
export interface IPageDataWithQueryResult extends IPageData {
result: IExecutionResult
}

Expand Down Expand Up @@ -127,19 +127,15 @@ export async function flush(): Promise<void> {
// them, a page might not exist anymore щ(゚Д゚щ)
// This is why we need this check
if (page) {
const body = await writePageData(
const result = await writePageData(
path.join(program.directory, `public`),
page
)

if (program?._?.[0] === `develop`) {
websocketManager.emitPageData({
...body.result,
id: pagePath,
result: {
data: body.result.data,
pageContext: body.result.pageContext,
},
result,
})
}
}
Expand Down
11 changes: 7 additions & 4 deletions packages/gatsby/src/utils/websocket-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { store } from "../redux"
import { Server as HTTPSServer } from "https"
import { Server as HTTPServer } from "http"
import fs from "fs"
import { readPageData } from "../utils/page-data"
import { readPageData, IPageDataWithQueryResult } from "../utils/page-data"
import telemetry from "gatsby-telemetry"
import url from "url"
import { createHash } from "crypto"
Expand All @@ -13,7 +13,7 @@ import socketIO from "socket.io"

export interface IPageQueryResult {
id: string
result: unknown // TODO: Improve this once we understand what the type is
result?: IPageDataWithQueryResult
}

export interface IStaticQueryResult {
Expand All @@ -35,10 +35,13 @@ const getCachedPageData = async (
const publicDir = path.join(program.directory, `public`)
if (pages.has(denormalizePagePath(pagePath)) || pages.has(pagePath)) {
try {
const pageData = await readPageData(publicDir, pagePath)
const pageData: IPageDataWithQueryResult = await readPageData(
publicDir,
pagePath
)

return {
result: pageData.result,
result: pageData,
id: pagePath,
}
} catch (err) {
Expand Down

0 comments on commit 38eed44

Please sign in to comment.