Skip to content

Commit

Permalink
implement getModule, load needed modules for a page in browser and …
Browse files Browse the repository at this point in the history
…for SSR
  • Loading branch information
pieh committed Jul 14, 2020
1 parent d558304 commit 9f67207
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby/cache-dir/gatsby-browser-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,5 @@ export {
useStaticQuery,
prefetchPathname,
}

export { getModule } from "./modules"
23 changes: 21 additions & 2 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prefetchHelper from "./prefetch"
import emitter from "./emitter"
import { setMatchPaths, findPath, findMatchPath } from "./find-path"
import { addModule, getModule } from "./modules"

/**
* Available resource loading statuses
Expand Down Expand Up @@ -199,8 +200,12 @@ export class BaseLoader {
}

let pageData = result.payload
const { componentChunkName } = pageData
return this.loadComponent(componentChunkName).then(component => {
const { componentChunkName, moduleDependencies = [] } = pageData

return Promise.all([
this.loadComponent(componentChunkName),
this.fetchModuleDependencies(moduleDependencies),
]).then(([component]) => {
const finalResult = { createdAt: new Date() }
let pageResources
if (!component) {
Expand Down Expand Up @@ -351,6 +356,20 @@ export class BaseLoader {
return appData
})
}

fetchModuleDependencies(moduleDependencies) {
return Promise.all(
moduleDependencies.map(moduleId => {
if (getModule(moduleId)) {
return Promise.resolve()
}

return this.loadComponent(moduleId, `modules`).then(module => {
addModule(moduleId, module)
})
})
)
}
}

const createComponentUrls = componentChunkName =>
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/cache-dir/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const modules = new Map()

export function addModule(moduleId, module) {
modules.set(moduleId, module)
}

export function getModule(moduleId) {
return modules.get(moduleId)
}
17 changes: 15 additions & 2 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
const { RouteAnnouncerProps } = require(`./route-announcer-props`)
const apiRunner = require(`./api-runner-ssr`)
const syncRequires = require(`$virtual/sync-requires`)
const { addModule, getModule } = require(`./modules`)
const { version: gatsbyVersion } = require(`gatsby/package.json`)

const stats = JSON.parse(
Expand Down Expand Up @@ -201,7 +202,19 @@ export default (pagePath, callback) => {

const appDataUrl = getAppDataUrl()

const { componentChunkName } = pageData
const { componentChunkName, moduleDependencies } = pageData

// make sure needed modules for the page are loaded in modules provider
if (moduleDependencies) {
moduleDependencies.forEach(moduleId => {
if (getModule(moduleId)) {
return
}

const module = syncRequires.modules[moduleId]
addModule(moduleId, module)
})
}

class RouteHandler extends React.Component {
render() {
Expand Down Expand Up @@ -274,7 +287,7 @@ export default (pagePath, callback) => {

// Create paths to scripts
let scriptsAndStyles = flatten(
[`app`, componentChunkName].map(s => {
[`app`, componentChunkName, ...(moduleDependencies || [])].map(s => {
const fetchKey = `assetsByChunkName[${s}]`

let chunks = get(stats, fetchKey)
Expand Down

0 comments on commit 9f67207

Please sign in to comment.