From 168351ef9b02bc51c0db7bc1233969c7b278c435 Mon Sep 17 00:00:00 2001 From: Dustin McCraw Date: Mon, 25 Oct 2021 15:07:32 -0700 Subject: [PATCH] Properly set the pathPrefix and assetPrefix in the pluginData (#33667) --- .../build-headers-program.js.snap | 2 ++ .../src/__tests__/build-headers-program.js | 23 ++++++++++++++++++- .../src/__tests__/routes.js | 5 +++- .../src/gatsby-node.js | 9 +++----- .../src/plugin-data.js | 7 +++--- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/__snapshots__/build-headers-program.js.snap b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/__snapshots__/build-headers-program.js.snap index 2ab297531110c..7d111f76c7c0f 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/__snapshots__/build-headers-program.js.snap +++ b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/__snapshots__/build-headers-program.js.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`build-headers-program with an assetPrefix 1`] = `"{\\"/*\\":[\\"X-Frame-Options: DENY\\",\\"X-XSS-Protection: 1; mode=block\\",\\"X-Content-Type-Options: nosniff\\",\\"Referrer-Policy: same-origin\\"],\\"/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/0-0180cd94ef2497ac7db8.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-templates-blog-post-js-517987eae96e75cddbe7.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-pages-404-js-53e6c51a5a7e73090f50.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-pages-index-js-0bdd01c77ee09ef0224c.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/webpack-runtime-acaa8994f1f704475e21.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/styles.1025963f4f2ec7abbad4.css\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/styles-565f081c8374bbda155f.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/app-f33c13590352da20930f.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/static/*\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/sw.js\\":[\\"Cache-Control: public, max-age=0, must-revalidate\\"],\\"/offline-plugin-app-shell-fallback/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/hi-folks/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/my-second-post/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/hello-world/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/404/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/test/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/404.html\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"]}"`; + exports[`build-headers-program with caching headers 1`] = `"{\\"/*\\":[\\"X-Frame-Options: DENY\\",\\"X-XSS-Protection: 1; mode=block\\",\\"X-Content-Type-Options: nosniff\\",\\"Referrer-Policy: same-origin\\"],\\"/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/0-0180cd94ef2497ac7db8.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-templates-blog-post-js-517987eae96e75cddbe7.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-pages-404-js-53e6c51a5a7e73090f50.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-pages-index-js-0bdd01c77ee09ef0224c.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/webpack-runtime-acaa8994f1f704475e21.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/styles.1025963f4f2ec7abbad4.css\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/styles-565f081c8374bbda155f.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/app-f33c13590352da20930f.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/static/*\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/sw.js\\":[\\"Cache-Control: public, max-age=0, must-revalidate\\"],\\"/offline-plugin-app-shell-fallback/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/hi-folks/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/my-second-post/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/hello-world/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/404/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/test/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/404.html\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"]}"`; exports[`build-headers-program with manifest['pages-manifest'] 1`] = `"{\\"/*\\":[\\"X-Frame-Options: DENY\\",\\"X-XSS-Protection: 1; mode=block\\",\\"X-Content-Type-Options: nosniff\\",\\"Referrer-Policy: same-origin\\"],\\"/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/0-0180cd94ef2497ac7db8.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-templates-blog-post-js-517987eae96e75cddbe7.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-pages-404-js-53e6c51a5a7e73090f50.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/component---src-pages-index-js-0bdd01c77ee09ef0224c.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/pages-manifest-ab11f09e0ca7ecd3b43e.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/webpack-runtime-acaa8994f1f704475e21.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/styles.1025963f4f2ec7abbad4.css\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/styles-565f081c8374bbda155f.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/app-f33c13590352da20930f.js\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/static/*\\":[\\"Cache-Control: public, max-age=31536000, immutable\\"],\\"/sw.js\\":[\\"Cache-Control: public, max-age=0, must-revalidate\\"],\\"/offline-plugin-app-shell-fallback/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/hi-folks/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/my-second-post/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/hello-world/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/404/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/test/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\"],\\"/\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"],\\"/404.html\\":[\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=script; nopush\\",\\"Link: ; rel=preload; as=fetch; crossorigin; nopush\\"]}"`; diff --git a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/build-headers-program.js b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/build-headers-program.js index 2fb4ea6bc1692..ef2248cf9a38c 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/build-headers-program.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/build-headers-program.js @@ -19,7 +19,7 @@ describe(`build-headers-program`, () => { const createPluginData = async () => { const tmpDir = await fs.mkdtemp( - path.join(os.tmpdir(), `abhi-plugin-fastly-`) + path.join(os.tmpdir(), `gatsby-plugin-gatsby-cloud-`) ) return { @@ -198,6 +198,7 @@ describe(`build-headers-program`, () => { ], }, pathPrefix: ``, + assetPrefix: ``, publicFolder: (...files) => path.join(tmpDir, ...files), } } @@ -349,4 +350,24 @@ describe(`build-headers-program`, () => { expect.any(Function) ) }) + + it(`with an assetPrefix`, async () => { + let pluginData = await createPluginData() + pluginData = { + ...pluginData, + assetPrefix: `http://cloud.gatsbyjs.io`, + } + + const pluginOptions = { + ...DEFAULT_OPTIONS, + } + + await buildHeadersProgram(pluginData, pluginOptions) + + const output = await fs.readFile( + pluginData.publicFolder(HEADERS_FILENAME), + `utf8` + ) + expect(output).toMatchSnapshot() + }) }) diff --git a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js index 0f2e1714af88a..d19871738881e 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js @@ -37,7 +37,6 @@ describe(`Routes IPC`, () => { onPostBuild( { - pathPrefix: ``, store: { getState() { return { @@ -66,6 +65,10 @@ describe(`Routes IPC`, () => { }, ], ]), + config: { + assetPath: ``, + pathPrefix: ``, + }, } }, }, diff --git a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js index 6c7f720510cb4..2cae64980f707 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js @@ -35,16 +35,13 @@ exports.onCreateWebpackConfig = ({ actions, stage }) => { }) } -exports.onPostBuild = async ( - { store, pathPrefix, getNodesByType }, - userPluginOptions -) => { - const pluginData = makePluginData(store, assetsManifest, pathPrefix) - +exports.onPostBuild = async ({ store, getNodesByType }, userPluginOptions) => { const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions } const { redirects, pageDataStats, nodes, pages } = store.getState() + const pluginData = makePluginData(store, assetsManifest) + /** * Emit via IPC routes for which pages are non SSG */ diff --git a/packages/gatsby-plugin-gatsby-cloud/src/plugin-data.js b/packages/gatsby-plugin-gatsby-cloud/src/plugin-data.js index ff4e0c8124712..74e05e1dc825e 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/plugin-data.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/plugin-data.js @@ -8,8 +8,8 @@ export function buildPrefixer(prefix, ...paths) { // This function assembles data across the manifests and store to match a similar // shape of `static-entry.js`. With it, we can build headers that point to the correct // hashed filenames and ensure we pull in the componentChunkName. -export default function makePluginData(store, assetsManifest, pathPrefix) { - const { program, pages, components } = store.getState() +export default function makePluginData(store, assetsManifest) { + const { program, pages, components, config } = store.getState() const publicFolder = buildPrefixer(program.directory, `public`) const functionsFolder = buildPrefixer( program.directory, @@ -28,7 +28,8 @@ export default function makePluginData(store, assetsManifest, pathPrefix) { components, manifest, program, - pathPrefix, + pathPrefix: config.pathPrefix, + assetPrefix: config.assetPrefix, publicFolder, functionsFolder, }