From ba922ae24b841cb9cd88df75f4658caf45056e2c Mon Sep 17 00:00:00 2001 From: Paul Mucur Date: Wed, 7 Dec 2022 09:44:06 +0000 Subject: [PATCH 1/2] fix(gatsby): Use path prefix when loading slice data As per the discussion in https://github.com/gatsbyjs/gatsby/discussions/37143, if an asset or path prefix has been set and prefix paths is enabled, slice data should loaded using that prefix. --- packages/gatsby/cache-dir/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/cache-dir/loader.js b/packages/gatsby/cache-dir/loader.js index eba4819db3b57..0c9e71ba61fc5 100644 --- a/packages/gatsby/cache-dir/loader.js +++ b/packages/gatsby/cache-dir/loader.js @@ -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) From 515b7d1d8b2c82919705c5fd2398f0a8d0b4ee7e Mon Sep 17 00:00:00 2001 From: pieh Date: Wed, 7 Dec 2022 14:44:00 +0100 Subject: [PATCH 2/2] add tests for page-data and slice-data being prefixed --- .../cypress/integration/asset-prefix.js | 29 +++++++++++++++++++ e2e-tests/path-prefix/gatsby-node.js | 6 ++++ .../path-prefix/src/components/layout.js | 5 ++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/e2e-tests/path-prefix/cypress/integration/asset-prefix.js b/e2e-tests/path-prefix/cypress/integration/asset-prefix.js index cd2ebf43cd2ea..866246512aba6 100644 --- a/e2e-tests/path-prefix/cypress/integration/asset-prefix.js +++ b/e2e-tests/path-prefix/cypress/integration/asset-prefix.js @@ -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`) diff --git a/e2e-tests/path-prefix/gatsby-node.js b/e2e-tests/path-prefix/gatsby-node.js index 6d4a90e7a0555..6761288ac9c30 100644 --- a/e2e-tests/path-prefix/gatsby-node.js +++ b/e2e-tests/path-prefix/gatsby-node.js @@ -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`), + }) +} diff --git a/e2e-tests/path-prefix/src/components/layout.js b/e2e-tests/path-prefix/src/components/layout.js index 2659ce0cf585a..013dfcbb67954 100644 --- a/e2e-tests/path-prefix/src/components/layout.js +++ b/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 }) => ( @@ -18,7 +17,7 @@ const Layout = ({ children }) => ( `} render={data => ( -
+