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(gatsby): Use path prefix when loading slice data #37202

Merged
merged 2 commits into from Dec 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions e2e-tests/path-prefix/cypress/integration/asset-prefix.js
Expand Up @@ -5,11 +5,40 @@ const assetPrefixExpression = new RegExp(`^${assetPrefix}`)
const assetPrefixMatcher = (chain, attr = `href`) =>
chain.should(`have.attr`, attr).and(`matches`, assetPrefixExpression)

beforeEach(() => {
cy.intercept(/page-data/).as("page-data")
cy.intercept(/slice-data/).as("slice-data")
})

describe(`assetPrefix`, () => {
beforeEach(() => {
cy.visit(`/`).waitForRouteChange()
})

it(`page-data is prefixed with asset prefix`, () => {
cy.wait("@page-data")

cy.get("@page-data").then((...intercepts) => {
expect(intercepts).to.have.length(1)

for (const intercept of intercepts) {
expect(intercept.request.url).to.match(assetPrefixExpression)
}
})
})

it(`slice-data is prefixed with asset prefix`, () => {
cy.wait("@slice-data")

cy.get("@slice-data").then((...intercepts) => {
expect(intercepts).to.have.length(1)

for (const intercept of intercepts) {
expect(intercept.request.url).to.match(assetPrefixExpression)
}
})
})

describe(`runtime`, () => {
it(`prefixes styles`, () => {
assetPrefixMatcher(cy.get(`head style[data-href]`), `data-href`)
Expand Down
6 changes: 6 additions & 0 deletions e2e-tests/path-prefix/gatsby-node.js
Expand Up @@ -5,3 +5,9 @@
*/

// You can delete this file if you're not using it
exports.createPages = async ({ actions }) => {
actions.createSlice({
id: `header`,
component: require.resolve(`./src/components/header.js`),
})
}
5 changes: 2 additions & 3 deletions e2e-tests/path-prefix/src/components/layout.js
@@ -1,8 +1,7 @@
import * as React from "react"
import PropTypes from "prop-types"
import { StaticQuery, graphql } from "gatsby"
import { StaticQuery, graphql, Slice } from "gatsby"

import Header from "./header"
import "./layout.css"

const Layout = ({ children }) => (
Expand All @@ -18,7 +17,7 @@ const Layout = ({ children }) => (
`}
render={data => (
<React.Fragment>
<Header siteTitle={data.site.siteMetadata.title} />
<Slice alias="header" siteTitle={data.site.siteMetadata.title} />
<div
style={{
margin: `0 auto`,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/loader.js
Expand Up @@ -327,7 +327,7 @@ export class BaseLoader {
return Promise.resolve({ sliceName, jsonPayload })
}

const url = `/slice-data/${sliceName}.json`
const url = `${__PATH_PREFIX__}/slice-data/${sliceName}.json`
return doFetch(url, `GET`).then(res => {
const jsonPayload = JSON.parse(res.responseText)

Expand Down