/
page-data.ts
251 lines (211 loc) · 6.36 KB
/
page-data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { walkStream as fsWalkStream, Entry } from "@nodelib/fs.walk"
import fs from "fs-extra"
import reporter from "gatsby-cli/lib/reporter"
import path from "path"
import { IGatsbyPage } from "../redux/types"
import { websocketManager } from "./websocket-manager"
import { isWebpackStatusPending } from "./webpack-status"
import { store } from "../redux"
import { IExecutionResult } from "../query/types"
interface IPageData {
componentChunkName: IGatsbyPage["componentChunkName"]
matchPath?: IGatsbyPage["matchPath"]
path: IGatsbyPage["path"]
staticQueryHashes: Array<string>
}
export interface IPageDataWithQueryResult extends IPageData {
result: IExecutionResult
}
export function fixedPagePath(pagePath: string): string {
return pagePath === `/` ? `index` : pagePath
}
export function reverseFixedPagePath(pageDataRequestPath: string): string {
return pageDataRequestPath === `index` ? `/` : pageDataRequestPath
}
function getFilePath(publicDir: string, pagePath: string): string {
return path.join(
publicDir,
`page-data`,
fixedPagePath(pagePath),
`page-data.json`
)
}
export async function readPageData(
publicDir: string,
pagePath: string
): Promise<IPageDataWithQueryResult> {
const filePath = getFilePath(publicDir, pagePath)
const rawPageData = await fs.readFile(filePath, `utf-8`)
return JSON.parse(rawPageData)
}
export async function removePageData(
publicDir: string,
pagePath: string
): Promise<void> {
const filePath = getFilePath(publicDir, pagePath)
if (fs.existsSync(filePath)) {
return await fs.remove(filePath)
}
return Promise.resolve()
}
export function pageDataExists(publicDir: string, pagePath: string): boolean {
return fs.existsSync(getFilePath(publicDir, pagePath))
}
export async function writePageData(
publicDir: string,
{
componentChunkName,
matchPath,
path: pagePath,
staticQueryHashes,
}: IPageData
): Promise<IPageDataWithQueryResult> {
const inputFilePath = path.join(
publicDir,
`..`,
`.cache`,
`json`,
`${pagePath.replace(/\//g, `_`)}.json`
)
const outputFilePath = getFilePath(publicDir, pagePath)
const result = await fs.readJSON(inputFilePath)
const body = {
componentChunkName,
path: pagePath,
matchPath,
result,
staticQueryHashes,
}
const bodyStr = JSON.stringify(body)
// transform asset size to kB (from bytes) to fit 64 bit to numbers
const pageDataSize = Buffer.byteLength(bodyStr) / 1000
store.dispatch({
type: `ADD_PAGE_DATA_STATS`,
payload: {
filePath: outputFilePath,
size: pageDataSize,
},
})
await fs.outputFile(outputFilePath, bodyStr)
return body
}
let isFlushPending = false
let isFlushing = false
export function isFlushEnqueued(): boolean {
return isFlushPending
}
export async function flush(): Promise<void> {
if (isFlushing) {
// We're already in the middle of a flush
return
}
isFlushPending = false
isFlushing = true
const {
pendingPageDataWrites,
pages,
program,
staticQueriesByTemplate,
queries,
} = store.getState()
const { pagePaths } = pendingPageDataWrites
const pagesToWrite = pagePaths.values()
for (const pagePath of pagesToWrite) {
const page = pages.get(pagePath)
// It's a gloomy day in Bombay, let me tell you a short story...
// Once upon a time, writing page-data.json files were atomic
// After this change (#24808), they are not and this means that
// between adding a pending write for a page and actually flushing
// them, a page might not exist anymore щ(゚Д゚щ)
// This is why we need this check
if (page) {
if (
program?._?.[0] === `develop` &&
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
) {
// check if already did run query for this page
// with query-on-demand we might have pending page-data write due to
// changes in static queries assigned to page template, but we might not
// have query result for it
const query = queries.trackedQueries.get(page.path)
if (!query) {
// this should not happen ever
throw new Error(
`We have a page, but we don't have registered query for it (???)`
)
}
if (query.dirty !== 0) {
// query results are not up to date, it's not safe to write page-data and emit results
continue
}
}
const staticQueryHashes =
staticQueriesByTemplate.get(page.componentPath) || []
const result = await writePageData(
path.join(program.directory, `public`),
{
...page,
staticQueryHashes,
}
)
if (program?._?.[0] === `develop`) {
websocketManager.emitPageData({
id: pagePath,
result,
})
}
}
store.dispatch({
type: `CLEAR_PENDING_PAGE_DATA_WRITE`,
payload: {
page: pagePath,
},
})
}
isFlushing = false
return
}
export function enqueueFlush(): void {
if (isWebpackStatusPending()) {
isFlushPending = true
} else {
flush()
}
}
export async function handleStalePageData(): Promise<void> {
if (!(await fs.pathExists(`public/page-data`))) {
return
}
// public directory might have stale page-data files from previous builds
// we get the list of those and compare against expected page-data files
// and remove ones that shouldn't be there anymore
const activity = reporter.activityTimer(`Cleaning up stale page-data`)
activity.start()
const pageDataFilesFromPreviousBuilds = await new Promise<Set<string>>(
(resolve, reject) => {
const results = new Set<string>()
const stream = fsWalkStream(`public/page-data`)
stream.on(`data`, (data: Entry) => {
if (data.name === `page-data.json`) {
results.add(data.path)
}
})
stream.on(`error`, e => {
reject(e)
})
stream.on(`end`, () => resolve(results))
}
)
const expectedPageDataFiles = new Set<string>()
store.getState().pages.forEach(page => {
expectedPageDataFiles.add(getFilePath(`public`, page.path))
})
const deletionPromises: Array<Promise<void>> = []
pageDataFilesFromPreviousBuilds.forEach(pageDataFilePath => {
if (!expectedPageDataFiles.has(pageDataFilePath)) {
deletionPromises.push(fs.remove(pageDataFilePath))
}
})
await Promise.all(deletionPromises)
activity.end()
}