From d9be04e778769ce5a99a4241de77c55551a3b636 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 19:09:11 +0200 Subject: [PATCH 01/11] fix bug, where ts files where not renamed correctly --- .changeset/sweet-bats-clap.md | 5 ++++ packages/integrations/cloudflare/src/index.ts | 23 ++++++++++--------- packages/integrations/cloudflare/src/util.ts | 6 ++--- .../cloudflare/test/directory-split.test.js | 1 + .../fixtures/split/src/pages/trpc/[trpc].ts | 7 ++++++ 5 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 .changeset/sweet-bats-clap.md create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts diff --git a/.changeset/sweet-bats-clap.md b/.changeset/sweet-bats-clap.md new file mode 100644 index 000000000000..9e33267a3df8 --- /dev/null +++ b/.changeset/sweet-bats-clap.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +fix bug where `.ts` files are not renamed to `.js diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 3dc237b7274d..6bc0d94a749e 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -21,15 +21,15 @@ interface BuildConfig { export function getAdapter(isModeDirectory: boolean): AstroAdapter { return isModeDirectory ? { - name: '@astrojs/cloudflare', - serverEntrypoint: '@astrojs/cloudflare/server.directory.js', - exports: ['onRequest', 'manifest'], - } + name: '@astrojs/cloudflare', + serverEntrypoint: '@astrojs/cloudflare/server.directory.js', + exports: ['onRequest', 'manifest'], + } : { - name: '@astrojs/cloudflare', - serverEntrypoint: '@astrojs/cloudflare/server.advanced.js', - exports: ['default'], - }; + name: '@astrojs/cloudflare', + serverEntrypoint: '@astrojs/cloudflare/server.advanced.js', + exports: ['default'], + }; } const SHIM = `globalThis.process = { @@ -138,7 +138,8 @@ export default function createIntegration(args?: Options): AstroIntegration { const fileName = entryPointsRouteData[index].component .replace('src/pages/', '') .replace('.astro', '.js') - .replace(/(\[\.\.\.)(\w+)(\])/g, (_match, _p1, p2) => { + .replace('.ts', '.js') + .replace(/(\[\.\.\.)(\w+)(\])/g, (_match, _p1, p2, _p3) => { return `[[${p2}]]`; }); @@ -184,8 +185,8 @@ export default function createIntegration(args?: Options): AstroIntegration { } // // // throw the server folder in the bin - const serverUrl = new URL(_buildConfig.server); - await fs.promises.rm(serverUrl, { recursive: true, force: true }); + // const serverUrl = new URL(_buildConfig.server); + // await fs.promises.rm(serverUrl, { recursive: true, force: true }); // move cloudflare specific files to the root const cloudflareSpecialFiles = ['_headers', '_redirects', '_routes.json']; diff --git a/packages/integrations/cloudflare/src/util.ts b/packages/integrations/cloudflare/src/util.ts index 120cb73343fa..4c1e89452051 100644 --- a/packages/integrations/cloudflare/src/util.ts +++ b/packages/integrations/cloudflare/src/util.ts @@ -9,9 +9,9 @@ export function getProcessEnvProxy() { console.warn( // NOTE: \0 prevents Vite replacement `Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization ` + - `as the Cloudflare platform only provides the environment variables per request. ` + - `Please move the environment variable access inside a function ` + - `that's only called after a request has been received.` + `as the Cloudflare platform only provides the environment variables per request. ` + + `Please move the environment variable access inside a function ` + + `that's only called after a request has been received.` ); }, } diff --git a/packages/integrations/cloudflare/test/directory-split.test.js b/packages/integrations/cloudflare/test/directory-split.test.js index 384543a4b896..cf43d244ca62 100644 --- a/packages/integrations/cloudflare/test/directory-split.test.js +++ b/packages/integrations/cloudflare/test/directory-split.test.js @@ -36,6 +36,7 @@ describe('Cloudflare SSR split', () => { expect(await fixture.pathExists('../functions/[person]/[car].js')).to.be.true; expect(await fixture.pathExists('../functions/files/[[path]].js')).to.be.true; expect(await fixture.pathExists('../functions/[language]/files/[[path]].js')).to.be.true; + expect(await fixture.pathExists('../functions/trpc/[trpc].js')).to.be.true; }); it('generates pre-rendered files', async () => { diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts b/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts new file mode 100644 index 000000000000..f64bf403e247 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts @@ -0,0 +1,7 @@ +export const prerender = false + +import type { APIRoute } from 'astro' + +export const all: APIRoute = (opts) => { + return "" +} From 02df9c3025fa5f397bf8ec7136e904f5938e0b51 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:17:00 +0200 Subject: [PATCH 02/11] try to make rename logic more robust --- packages/integrations/cloudflare/src/index.ts | 71 ++++++++++++------- .../fixtures/split/src/pages/javascript.js | 0 .../fixtures/split/src/pages/test.json.ts | 0 3 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/javascript.js create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/test.json.ts diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 6bc0d94a749e..e5314746a1b7 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -107,10 +107,10 @@ export default function createIntegration(args?: Options): AstroIntegration { const entryPointsRouteData = [..._entryPoints.keys()]; const entryPointsURL = [..._entryPoints.values()]; const entryPaths = entryPointsURL.map((entry) => fileURLToPath(entry)); - const outputDir = fileURLToPath(new URL('.astro', _buildConfig.server)); + const outputUrl = new URL('$astro', _buildConfig.server) + const outputDir = fileURLToPath(outputUrl); - // NOTE: AFAIK, esbuild keeps the order of the entryPoints array - const { outputFiles } = await esbuild.build({ + await esbuild.build({ target: 'es2020', platform: 'browser', conditions: ['workerd', 'worker', 'browser'], @@ -126,29 +126,52 @@ export default function createIntegration(args?: Options): AstroIntegration { logOverride: { 'ignored-bare-import': 'silent', }, - write: false, }); - // loop through all bundled files and write them to the functions folder - for (const [index, outputFile] of outputFiles.entries()) { - // we need to make sure the filename in the functions folder - // matches to cloudflares routing capabilities (see their docs) - // IN: src/pages/[language]/files/[...path].astro - // OUT: [language]/files/[[path]].js - const fileName = entryPointsRouteData[index].component - .replace('src/pages/', '') - .replace('.astro', '.js') - .replace('.ts', '.js') - .replace(/(\[\.\.\.)(\w+)(\])/g, (_match, _p1, p2, _p3) => { - return `[[${p2}]]`; - }); - - const fileUrl = new URL(fileName, functionsUrl); - const newFileDir = dirname(fileURLToPath(fileUrl)); - if (!fs.existsSync(newFileDir)) { - fs.mkdirSync(newFileDir, { recursive: true }); - } - await fs.promises.writeFile(fileUrl, outputFile.contents); + const outputFiles: Array = ( + await glob(`**/*`, { + cwd: outputDir, + filesOnly: true, + }) + ) + + console.log(outputFiles) + + // loop through all new bundled files and write them to the functions folder + for (const outputFile of outputFiles) { + + // split the path into an array + const path = outputFile.split('/'); + + // replace dynamic path with [path] + const pathWithDynamics = path.map((segment) => segment.replace(/(\_)(\w+)(\_)/g, (_, __, prop, ___) => { + return `[${prop}]`; + })); + + // replace nested dynamic path with [[path]] + const pathWithNestedDynamics = pathWithDynamics.map((segment) => segment.replace(/(\_\-\-\-)(\w+)(\_)/g, (_, __, prop, ___) => { + return `[[${prop}]]`; + })) + + // remove original file extension + const pathReversed = pathWithNestedDynamics.reverse(); + pathReversed[0] = pathReversed[0] + .replace('entry.', '') + .replace(/(.*)\.(\w+)\.(\w+)$/g, (_, fileName, oldExt, newExt) => { + return `${fileName}.${newExt}`; + }) + + const finalSegments = pathReversed.reverse(); + const finalDirPath = finalSegments.slice(0, -1).join('/'); + const finalPath = finalSegments.join('/'); + + const newDirUrl = new URL(finalDirPath, functionsUrl); + await fs.promises.mkdir(newDirUrl, { recursive: true }) + + const oldFileUrl = new URL(`$astro/${outputFile}`, outputUrl); + const newFileUrl = new URL(finalPath, functionsUrl); + + await fs.promises.rename(oldFileUrl, newFileUrl); } } else { const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)); diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/javascript.js b/packages/integrations/cloudflare/test/fixtures/split/src/pages/javascript.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/test.json.ts b/packages/integrations/cloudflare/test/fixtures/split/src/pages/test.json.ts new file mode 100644 index 000000000000..e69de29bb2d1 From c051389e51588080021c1c6714f8bcb74ca3adb3 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:20:56 +0200 Subject: [PATCH 03/11] remove log --- packages/integrations/cloudflare/src/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index e5314746a1b7..6c9d5a9c26a8 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -104,7 +104,6 @@ export default function createIntegration(args?: Options): AstroIntegration { } if (isModeDirectory && _buildConfig.split) { - const entryPointsRouteData = [..._entryPoints.keys()]; const entryPointsURL = [..._entryPoints.values()]; const entryPaths = entryPointsURL.map((entry) => fileURLToPath(entry)); const outputUrl = new URL('$astro', _buildConfig.server) @@ -135,8 +134,6 @@ export default function createIntegration(args?: Options): AstroIntegration { }) ) - console.log(outputFiles) - // loop through all new bundled files and write them to the functions folder for (const outputFile of outputFiles) { From 653a594d826bbb0f4034b3d8ae3674789989c587 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:24:04 +0200 Subject: [PATCH 04/11] update tests --- packages/integrations/cloudflare/test/directory-split.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/integrations/cloudflare/test/directory-split.test.js b/packages/integrations/cloudflare/test/directory-split.test.js index cf43d244ca62..6e6b0bfe29aa 100644 --- a/packages/integrations/cloudflare/test/directory-split.test.js +++ b/packages/integrations/cloudflare/test/directory-split.test.js @@ -37,6 +37,8 @@ describe('Cloudflare SSR split', () => { expect(await fixture.pathExists('../functions/files/[[path]].js')).to.be.true; expect(await fixture.pathExists('../functions/[language]/files/[[path]].js')).to.be.true; expect(await fixture.pathExists('../functions/trpc/[trpc].js')).to.be.true; + expect(await fixture.pathExists('../functions/javascript.js')).to.be.true; + expect(await fixture.pathExists('../functions/test.json.js')).to.be.true; }); it('generates pre-rendered files', async () => { From fc7e764c7e441c0cc6913cd6ef0552f84292a992 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:26:47 +0200 Subject: [PATCH 05/11] update changeset --- .changeset/sweet-bats-clap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sweet-bats-clap.md b/.changeset/sweet-bats-clap.md index 9e33267a3df8..ee3f1761076a 100644 --- a/.changeset/sweet-bats-clap.md +++ b/.changeset/sweet-bats-clap.md @@ -2,4 +2,4 @@ '@astrojs/cloudflare': patch --- -fix bug where `.ts` files are not renamed to `.js +fix bug where `.ts` files are not renamed to `.js` From 7d43623dfbb9bc7ec1fff2c32a3162003b997701 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:28:24 +0200 Subject: [PATCH 06/11] cleanup --- packages/integrations/cloudflare/src/index.ts | 4 ++-- packages/integrations/cloudflare/src/util.ts | 6 +++--- .../test/fixtures/split/src/pages/trpc/[trpc].ts | 7 ------- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 6c9d5a9c26a8..1c520196646a 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -205,8 +205,8 @@ export default function createIntegration(args?: Options): AstroIntegration { } // // // throw the server folder in the bin - // const serverUrl = new URL(_buildConfig.server); - // await fs.promises.rm(serverUrl, { recursive: true, force: true }); + const serverUrl = new URL(_buildConfig.server); + await fs.promises.rm(serverUrl, { recursive: true, force: true }); // move cloudflare specific files to the root const cloudflareSpecialFiles = ['_headers', '_redirects', '_routes.json']; diff --git a/packages/integrations/cloudflare/src/util.ts b/packages/integrations/cloudflare/src/util.ts index 4c1e89452051..120cb73343fa 100644 --- a/packages/integrations/cloudflare/src/util.ts +++ b/packages/integrations/cloudflare/src/util.ts @@ -9,9 +9,9 @@ export function getProcessEnvProxy() { console.warn( // NOTE: \0 prevents Vite replacement `Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization ` + - `as the Cloudflare platform only provides the environment variables per request. ` + - `Please move the environment variable access inside a function ` + - `that's only called after a request has been received.` + `as the Cloudflare platform only provides the environment variables per request. ` + + `Please move the environment variable access inside a function ` + + `that's only called after a request has been received.` ); }, } diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts b/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts index f64bf403e247..e69de29bb2d1 100644 --- a/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/trpc/[trpc].ts @@ -1,7 +0,0 @@ -export const prerender = false - -import type { APIRoute } from 'astro' - -export const all: APIRoute = (opts) => { - return "" -} From f22a4c56eca80b305a29906a3b8b52b1e280ef06 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:34:54 +0200 Subject: [PATCH 07/11] fix lint --- packages/integrations/cloudflare/src/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 1c520196646a..2223e4d0ba9a 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -3,7 +3,6 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'ast import esbuild from 'esbuild'; import * as fs from 'fs'; import * as os from 'os'; -import { dirname } from 'path'; import glob from 'tiny-glob'; import { fileURLToPath, pathToFileURL } from 'url'; @@ -141,12 +140,12 @@ export default function createIntegration(args?: Options): AstroIntegration { const path = outputFile.split('/'); // replace dynamic path with [path] - const pathWithDynamics = path.map((segment) => segment.replace(/(\_)(\w+)(\_)/g, (_, __, prop, ___) => { + const pathWithDynamics = path.map((segment) => segment.replace(/(\_)(\w+)(\_)/g, (_, __, prop) => { return `[${prop}]`; })); // replace nested dynamic path with [[path]] - const pathWithNestedDynamics = pathWithDynamics.map((segment) => segment.replace(/(\_\-\-\-)(\w+)(\_)/g, (_, __, prop, ___) => { + const pathWithNestedDynamics = pathWithDynamics.map((segment) => segment.replace(/(\_\-\-\-)(\w+)(\_)/g, (_, __, prop) => { return `[[${prop}]]`; })) From 8b17eb2be5e057f34c6d1306ff9ea029af5bb14b Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:42:18 +0200 Subject: [PATCH 08/11] debug windows tests --- packages/integrations/cloudflare/src/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 2223e4d0ba9a..74de8558c36b 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -133,12 +133,14 @@ export default function createIntegration(args?: Options): AstroIntegration { }) ) + console.log(outputFiles) + // loop through all new bundled files and write them to the functions folder for (const outputFile of outputFiles) { // split the path into an array const path = outputFile.split('/'); - + console.log(path) // replace dynamic path with [path] const pathWithDynamics = path.map((segment) => segment.replace(/(\_)(\w+)(\_)/g, (_, __, prop) => { return `[${prop}]`; @@ -166,7 +168,7 @@ export default function createIntegration(args?: Options): AstroIntegration { const oldFileUrl = new URL(`$astro/${outputFile}`, outputUrl); const newFileUrl = new URL(finalPath, functionsUrl); - + console.log(oldFileUrl, newFileUrl) await fs.promises.rename(oldFileUrl, newFileUrl); } } else { From 27d69b79d8e632d82b5ef0bff1f411d31674a470 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 20:51:44 +0200 Subject: [PATCH 09/11] fix windows support --- packages/integrations/cloudflare/src/index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 74de8558c36b..610a61df2171 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -3,6 +3,7 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'ast import esbuild from 'esbuild'; import * as fs from 'fs'; import * as os from 'os'; +import { sep } from 'path'; import glob from 'tiny-glob'; import { fileURLToPath, pathToFileURL } from 'url'; @@ -133,14 +134,12 @@ export default function createIntegration(args?: Options): AstroIntegration { }) ) - console.log(outputFiles) - // loop through all new bundled files and write them to the functions folder for (const outputFile of outputFiles) { // split the path into an array - const path = outputFile.split('/'); - console.log(path) + const path = outputFile.split(sep); + // replace dynamic path with [path] const pathWithDynamics = path.map((segment) => segment.replace(/(\_)(\w+)(\_)/g, (_, __, prop) => { return `[${prop}]`; @@ -168,7 +167,6 @@ export default function createIntegration(args?: Options): AstroIntegration { const oldFileUrl = new URL(`$astro/${outputFile}`, outputUrl); const newFileUrl = new URL(finalPath, functionsUrl); - console.log(oldFileUrl, newFileUrl) await fs.promises.rename(oldFileUrl, newFileUrl); } } else { From 509de8d9d36f2b77d87b940fd50c9453d403552e Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Mon, 3 Jul 2023 21:21:20 +0200 Subject: [PATCH 10/11] fix cloudflare directory code --- packages/integrations/cloudflare/src/server.directory.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/integrations/cloudflare/src/server.directory.ts b/packages/integrations/cloudflare/src/server.directory.ts index f9f71a33b5b8..25a13958df10 100644 --- a/packages/integrations/cloudflare/src/server.directory.ts +++ b/packages/integrations/cloudflare/src/server.directory.ts @@ -24,7 +24,13 @@ export function createExports(manifest: SSRManifest) { const { pathname } = new URL(request.url); // static assets fallback, in case default _routes.json is not used if (manifest.assets.has(pathname)) { - return next(request); + // we need this so the page does not error + // https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function + return (runtimeEnv.env as unknown & { + ASSETS: { + fetch: typeof fetch; + }; + }).ASSETS.fetch(request); } let routeData = app.match(request, { matchNotFound: true }); From 45c39e5108b9c83ea78035cef7159f875c33eb68 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 5 Jul 2023 07:32:02 +0200 Subject: [PATCH 11/11] fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect fix bug for cloudflare assets redirect --- .changeset/shiny-parrots-clap.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shiny-parrots-clap.md diff --git a/.changeset/shiny-parrots-clap.md b/.changeset/shiny-parrots-clap.md new file mode 100644 index 000000000000..302870935d0f --- /dev/null +++ b/.changeset/shiny-parrots-clap.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +fix bug where asset redirects caused Cloudflare error