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

[WIP] Replace global data.json with page-data.json per page #13004

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
4e6091e
refactor query runner to allow running static/page queries separately
Moocar Mar 24, 2019
071320f
documenting
Moocar Mar 26, 2019
722c45a
Create queue configurations for build/develop
Moocar Mar 27, 2019
8868072
moved page-query-runner to index
Moocar Mar 24, 2019
d7dc21e
move src/internal-plugins/query-runner to src/query
Moocar Mar 27, 2019
6f036a5
add match paths json
Moocar Mar 23, 2019
330b2ba
write page data on query completion
Moocar Mar 24, 2019
034cc78
run page queries after bootstrap
Moocar Mar 25, 2019
9caff6a
use page data instead of data.json
Moocar Mar 25, 2019
d64e6bf
add webpack compilation hash
Moocar Mar 25, 2019
2b1b3c6
use compilation hash to refresh production-app
Moocar Mar 25, 2019
64bd2b5
remove develop-html (use build-html)
Moocar Mar 25, 2019
f3292ff
query-handler -> job handler. And ref ws manager from develop.js
Moocar Mar 25, 2019
738fbb6
invoke dev-404 correctly. Fix loader to handle develop mode
Moocar Mar 26, 2019
854e7b2
post merge fixes
Moocar Mar 28, 2019
54f79eb
only refresh page on compilation hash in production
Moocar Mar 28, 2019
05fe38a
get fetch 404 working
Moocar Mar 28, 2019
76e0b29
serve pages from gatsby develop
Moocar Mar 28, 2019
f35ae9c
404 page is fully working. Introduced loadPage, getPage
Moocar Mar 28, 2019
21aa5b8
bump gatsbygram gatsby version
Moocar Mar 29, 2019
7a12d71
misc fixes
Moocar Mar 30, 2019
ae210a9
fix 404 loader race condition
Moocar Mar 31, 2019
13204aa
add dev loader for 404 pages
Moocar Mar 31, 2019
ae5ddab
deprecate loader functions (getResources...)
Moocar Apr 1, 2019
406f26d
app load pages in parallel
Moocar Apr 1, 2019
21c325e
cleaning up for PR review
Moocar Apr 1, 2019
8a0ed10
remove failed history stuff
Moocar Apr 1, 2019
dcd65ea
cleanup for PR
Moocar Apr 1, 2019
6dd6e1e
standardize staticQueryResult.result
Moocar Apr 1, 2019
6d7504e
dataAndContext => pageData
Moocar Apr 1, 2019
a148f19
add activity back to build html pages
Moocar Apr 1, 2019
ee6a4c8
remove jsonName from Gatsby
Moocar Apr 1, 2019
1ed5e9f
add loadPageDataSync for guess
Moocar Apr 1, 2019
2c9b446
clean up bootstrap
Moocar Apr 1, 2019
ca2b0c4
explicitly save db state post bootstrap and develop
Moocar Apr 1, 2019
2af3786
watch for page deletes during develop
Moocar Apr 1, 2019
4b158e6
move query running out of bootstrap
Moocar Apr 1, 2019
7110cb7
ascertainPort => selectPort
Moocar Apr 1, 2019
2ed38d5
processPageQueries and processStaticQueries
Moocar Apr 1, 2019
8e90225
rewrite compilation hashes when changed
Moocar Apr 1, 2019
227b683
perform page queries before js compilation in develop
Moocar Apr 1, 2019
19fbb00
daemon -> listener
Moocar Apr 1, 2019
bc7118f
minor
Moocar Apr 1, 2019
ce46689
Merge branch 'master' into per-page-manifest2
Moocar Apr 3, 2019
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
Prev Previous commit
Next Next commit
invoke dev-404 correctly. Fix loader to handle develop mode
  • Loading branch information
Moocar committed Apr 1, 2019
commit 738fbb6531860b23d0ab1ff7312701b7ba7cb489
24 changes: 13 additions & 11 deletions packages/gatsby/cache-dir/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import domReady from "domready"
import socketIo from "./socketIo"
import emitter from "./emitter"
import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
import loader, { setApiRunnerForLoader, postInitialRenderWork } from "./loader"
import loader, { setApiRunnerForLoader } from "./loader"
import syncRequires from "./sync-requires"
import pages from "./pages.json"

window.___emitter = emitter
setApiRunnerForLoader(apiRunner)
Expand Down Expand Up @@ -46,19 +45,22 @@ apiRunnerAsync(`onClientEntry`).then(() => {
ReactDOM.render
)[0]

loader.addPagesArray(pages)
loader.addDevRequires(syncRequires)

loader.getResourcesForPathname(window.location.pathname).then(() => {
const preferDefault = m => (m && m.default) || m
let Root = preferDefault(require(`./root`))
domReady(() => {
renderer(<Root />, rootElement, () => {
postInitialRenderWork()
apiRunner(`onInitialClientRender`)
loader
.getResourcesForPathname(window.location.pathname)
.then(() => {
loader.getResourcesForPathname(`/dev-404-page/`)
})
.then(() => {
const preferDefault = m => (m && m.default) || m
let Root = preferDefault(require(`./root`))
domReady(() => {
renderer(<Root />, rootElement, () => {
apiRunner(`onInitialClientRender`)
})
})
})
})
})

function supportsServiceWorkers(location, navigator) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/json-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class JSONStore extends React.Component {
render() {
const data = this.state.pageQueryData[getPathFromProps(this.props)]
// eslint-disable-next-line
const { pages, ...propsWithoutPages } = this.props
const { ...propsWithoutPages } = this.props
if (!data) {
return <div />
}
Expand Down
123 changes: 45 additions & 78 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,7 @@ const onPostPrefetch = url => {
}
}

/**
* Check if we should fallback to resources for 404 page if resources for a page are not found
*
* We can't do that when we don't have full pages manifest - we don't know if page exist or not if we don't have it.
* We also can't do that on initial render / mount in case we just can't load resources needed for first page.
* Not falling back to 404 resources will cause "EnsureResources" component to handle scenarios like this with
* potential reload
* @param {string} path Path to a page
*/
// TODO review to make sure this makes sense
const shouldFallbackTo404Resources = path => path !== `/404.html`

// Note we're not actively using the path data atm. There
Expand Down Expand Up @@ -255,15 +247,14 @@ const queue = {
return true
}

// TODO
// if (
// process.env.NODE_ENV !== `production` &&
// process.env.NODE_ENV !== `test`
// ) {
// devGetPageData(page.path)
// }
if (
process.env.NODE_ENV !== `production` &&
process.env.NODE_ENV !== `test`
) {
// Ensure latest version of page data is in the JSON store
devGetPageData(realPath)
}

// Prefetch resources.
if (process.env.NODE_ENV === `production`) {
const pageDataUrl = makePageDataUrl(realPath)
prefetchHelper(pageDataUrl)
Expand Down Expand Up @@ -299,9 +290,12 @@ const queue = {
)
if (realPath in pathScriptsCache) {
return pathScriptsCache[realPath]
} else if (shouldFallbackTo404Resources(realPath)) {
return queue.getResourcesForPathnameSync(`/404.html`)
} else {
}
// TODO add this back in?
// else if (shouldFallbackTo404Resources(realPath)) {
// return queue.getResourcesForPathnameSync(`/404.html`)
// }
else {
return null
}
},
Expand Down Expand Up @@ -363,28 +357,48 @@ const queue = {

const { componentChunkName } = pageData

if (process.env.NODE_ENV !== `production`) {
const finalResolve = component => {
const page = {
componentChunkName: pageData.componentChunkName,
path: pageData.path,
compilationHash: pageData.compilationHash,
}

const jsonData = {
data: pageData.data,
pageContext: pageData.pageContext,
}

const pageResources = {
component: syncRequires.components[page.componentChunkName],
component,
json: jsonData,
page,
}

// Add to the cache.
pathScriptsCache[realPath] = pageResources
devGetPageData(page.path).then(pageData => {
emitter.emit(`onPostLoadPageResources`, {
page,
pageResources,
})
// Tell plugins the path has been successfully prefetched
// TODO onPostPrefetch(makePageDataUrl(realPath))
resolve(pageResources)

resolve(pageResources)
emitter.emit(`onPostLoadPageResources`, {
page,
pageResources,
})

// Tell plugins the path has been successfully prefetched
const pageDataUrl = makePageDataUrl(realPath)
const componentUrls = createComponentUrls(componentChunkName)
const resourceUrls = [pageDataUrl].concat(componentUrls)
onPostPrefetch({
path: rawPath,
resourceUrls,
})
}

if (process.env.NODE_ENV !== `production`) {
// Ensure latest version of page data is in the JSON store
devGetPageData(realPath)
const component = syncRequires.components[pageData.componentChunkName]
finalResolve(component)
} else {
console.log(`getting page component: [${componentChunkName}]`)
cachedFetch(componentChunkName, fetchComponent)
Expand All @@ -395,59 +409,12 @@ const queue = {
resolve(null)
return
}

const page = {
componentChunkName,
path: pageData.path,
compilationHash: pageData.compilationHash,
}

const jsonData = {
data: pageData.data,
pageContext: pageData.pageContext,
}

const pageResources = {
component,
json: jsonData,
page,
}

// TODO
// pageResources.page.jsonURL = createJsonURL(
// jsonDataPaths[page.jsonName]
// )
pathScriptsCache[realPath] = pageResources
resolve(pageResources)

emitter.emit(`onPostLoadPageResources`, {
page,
pageResources,
})

// Tell plugins the path has been successfully prefetched
const pageDataUrl = makePageDataUrl(realPath)
const componentUrls = createComponentUrls(componentChunkName)
const resourceUrls = [pageDataUrl].concat(componentUrls)
onPostPrefetch({
path: rawPath,
resourceUrls,
})
finalResolve(component)
})
}
}),
}

// TODO This doesn't make sense anymore
export const postInitialRenderWork = () => {
inInitialRender = false
if (process.env.NODE_ENV === `production`) {
// We got all resources needed for first mount,
// we can fetch resources for all pages.
fetchPageResourceMap()
}
}

export const setApiRunnerForLoader = runner => {
apiRunner = runner
disableCorePrefetching = apiRunner(`disableCorePrefetching`)
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import emitter from "./emitter"
import PageRenderer from "./page-renderer"
import asyncRequires from "./async-requires"
import loader, { setApiRunnerForLoader, postInitialRenderWork } from "./loader"
import loader, { setApiRunnerForLoader } from "./loader"
import EnsureResources from "./ensure-resources"

window.asyncRequires = asyncRequires
Expand All @@ -22,7 +22,6 @@ window.___compilationHash = window.pageData.compilationHash
console.log(`compilation hash`, window.___compilationHash)

loader.addPageData([window.pageData])
// loader.addDataPaths({ [window.page.jsonName]: window.dataPath })
loader.addProdRequires(asyncRequires)
setApiRunnerForLoader(apiRunner)

Expand Down
27 changes: 10 additions & 17 deletions packages/gatsby/cache-dir/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from "./navigation"
import { apiRunner } from "./api-runner-browser"
import syncRequires from "./sync-requires"
import pages from "./pages.json"
import loader from "./loader"
import JSONStore from "./json-store"
import EnsureResources from "./ensure-resources"
Expand Down Expand Up @@ -41,7 +40,7 @@ class RouteHandler extends React.Component {

// check if page exists - in dev pages are sync loaded, it's safe to use
// loader.getPage
let page = loader.getPage(location.pathname)
let page = loader.getResourcesForPathnameSync(location.pathname)

if (page) {
return (
Expand All @@ -52,24 +51,23 @@ class RouteHandler extends React.Component {
location={location}
shouldUpdateScroll={shouldUpdateScroll}
>
<JSONStore
pages={pages}
{...this.props}
{...locationAndPageResources}
/>
<JSONStore {...this.props} {...locationAndPageResources} />
</ScrollContext>
</RouteUpdates>
)}
</EnsureResources>
)
} else {
const dev404Page = pages.find(p => /^\/dev-404-page\/?$/.test(p.path))
const Dev404Page = syncRequires.components[dev404Page.componentChunkName]
const dev404PageData = loader.getResourcesForPathnameSync(
`/dev-404-page/`
)
const Dev404Page =
syncRequires.components[dev404PageData.page.componentChunkName]

if (!loader.getPage(`/404.html`)) {
if (!loader.getResourcesForPathnameSync(`/404.html`)) {
return (
<RouteUpdates location={location}>
<Dev404Page pages={pages} {...this.props} />
<Dev404Page {...this.props} />
</RouteUpdates>
)
}
Expand All @@ -79,13 +77,8 @@ class RouteHandler extends React.Component {
{locationAndPageResources => (
<RouteUpdates location={location}>
<Dev404Page
pages={pages}
custom404={
<JSONStore
pages={pages}
{...this.props}
{...locationAndPageResources}
/>
<JSONStore {...this.props} {...locationAndPageResources} />
}
{...this.props}
/>
Expand Down
2 changes: 0 additions & 2 deletions packages/gatsby/src/commands/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = async program => {
schema,
config,
babelrc,
jsonDataPaths,
pages,
components,
staticQueryComponents,
Expand All @@ -39,7 +38,6 @@ module.exports = async program => {
// set some globals to make life easier
_.context.babelrc = babelrc
_.context.components = components
_.context.dataPaths = jsonDataPaths
_.context.getNode = getNode
_.context.getNodes = getNodes
_.context.getNodesByType = getNodesByType
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ function saveState() {
`nodes`,
`status`,
`componentDataDependencies`,
`jsonDataPaths`,
`components`,
`staticQueryComponents`,
])
Expand Down
Loading