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

Fix loading the layout component in the frontend #3049

Merged
merged 1 commit into from
Nov 28, 2017
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
13 changes: 8 additions & 5 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ const fetchResource = (resourceName, cb = () => {}) => {
})
} else {
// Find resource
const resourceFunction =
resourceName.slice(0, 12) === `component---`
? asyncRequires.components[resourceName] ||
asyncRequires.layouts[resourceName]
: asyncRequires.json[resourceName]
let resourceFunction
if (resourceName.slice(0, 12) === `component---`) {
resourceFunction = asyncRequires.components[resourceName]
} else if (resourceName.slice(0, 9) === `layout---`) {
resourceFunction = asyncRequires.layouts[resourceName]
} else {
resourceFunction = asyncRequires.json[resourceName]
}

// Download the resource
resourceFunction((err, executeChunk) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const writePages = async () => {
...mem,
{
componentChunkName,
layout,
layout: layoutOjb ? layoutOjb.machineId : layout,
layoutComponentChunkName: layoutOjb && layoutOjb.componentChunkName,
jsonName,
path,
Expand Down Expand Up @@ -86,7 +86,7 @@ const preferDefault = m => m && m.default || m
syncRequires += `exports.layouts = {\n${pageLayouts
.map(
l =>
` "${l.id}": preferDefault(require("${
` "${l.machineId}": preferDefault(require("${
l.componentWrapperPath
}"))`
)
Expand Down Expand Up @@ -124,7 +124,7 @@ const preferDefault = m => m && m.default || m
asyncRequires += `exports.layouts = {\n${pageLayouts
.map(
l =>
` "${l.id}": require("gatsby-module-loader?name=${
` "${l.machineId}": require("gatsby-module-loader?name=${
l.componentChunkName
}!${l.componentWrapperPath}")`
)
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const gatsbyConfigSchema = Joi.object().keys({
export const layoutSchema = Joi.object()
.keys({
id: Joi.string().required(),
machineId: Joi.string().required(),
component: Joi.string().required(),
componentWrapperPath: Joi.string().required(),
componentChunkName: Joi.string().required(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Object {
"internalComponentName": "Component-layout-Index",
"isLayout": true,
"jsonName": "layout-index.json",
"machineId": "layout---index",
"pluginCreator___NODE": "Plugin test",
},
"plugin": Object {
Expand All @@ -36,6 +37,7 @@ Array [
"internalComponentName": "Component-layout-Index",
"isLayout": true,
"jsonName": "layout-index.json",
"machineId": "layout---index",
"pluginCreator___NODE": "Plugin test",
},
]
Expand All @@ -52,6 +54,7 @@ Object {
"internalComponentName": "Component-layout-Index",
"isLayout": true,
"jsonName": "layout-index.json",
"machineId": "layout---index",
"pluginCreator___NODE": "Plugin test",
},
"plugin": Object {
Expand All @@ -73,6 +76,7 @@ Array [
"internalComponentName": "Component-layout-Index",
"isLayout": true,
"jsonName": "layout-index.json",
"machineId": "layout---index",
"pluginCreator___NODE": "Plugin test",
},
]
Expand All @@ -89,6 +93,7 @@ Object {
"internalComponentName": "Component-layout-TestId",
"isLayout": true,
"jsonName": "layout-test-id.json",
"machineId": "layout---test-id",
"pluginCreator___NODE": "Plugin test",
},
"plugin": Object {
Expand All @@ -110,6 +115,7 @@ Array [
"internalComponentName": "Component-layout-TestId",
"isLayout": true,
"jsonName": "layout-test-id.json",
"machineId": "layout---test-id",
"pluginCreator___NODE": "Plugin test",
},
]
Expand All @@ -126,6 +132,7 @@ Array [
"internalComponentName": "Component-layout-Index",
"isLayout": true,
"jsonName": "layout-index.json",
"machineId": "layout---index",
"pluginCreator___NODE": "Plugin test",
},
Object {
Expand All @@ -137,6 +144,7 @@ Array [
"internalComponentName": "Component-layout-Pizza",
"isLayout": true,
"jsonName": "layout-pizza.json",
"machineId": "layout---pizza",
"pluginCreator___NODE": "Plugin test",
},
]
Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type PageInput = {
}
type LayoutInput = {
id?: string,
machineId?: string,
component: string,
layout?: string,
context?: Object,
Expand Down Expand Up @@ -187,6 +188,9 @@ actions.createLayout = (
traceId?: string
) => {
let id = layout.id || path.parse(layout.component).name
// Add a "machine" id as a universal ID to differentiate layout from
// page components.
const machineId = `layout---${id}`
let componentWrapperPath = joinPath(
store.getState().program.directory,
`.cache`,
Expand All @@ -196,6 +200,7 @@ actions.createLayout = (

let internalLayout: Layout = {
id,
machineId,
componentWrapperPath,
isLayout: true,
jsonName: `layout-${_.kebabCase(id)}.json`,
Expand Down