From 1a59185ddd393bf8894ec0c981b26d6fecdb3c67 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Fri, 30 Jun 2023 11:09:21 +0200 Subject: [PATCH 1/3] feature(astrojs/cloudflare): add support for `splitted` SSR bundles (#7464) * initial commit * try to fix windows * output files directly into the correct folder * allow for rest parameters * use fixed hook * improve tests * apply doc's team suggestions for README Co-authored-by: Sarah Rainsberger * try to fix prerendering * apply doc's team suggestion for changeset Co-authored-by: Sarah Rainsberger * bump to minor * readme update * resolve review comments * optimize memory allocation * resolve review comments * add removed link, to make sure old docs keep same * resolve comment Co-authored-by: Sarah Rainsberger --------- Co-authored-by: Sarah Rainsberger Co-authored-by: Chris Swithinbank --- .changeset/healthy-books-study.md | 7 + packages/integrations/cloudflare/README.md | 6 +- packages/integrations/cloudflare/package.json | 3 +- packages/integrations/cloudflare/src/index.ts | 145 ++++++++++++------ .../cloudflare/src/server.directory.ts | 2 +- .../cloudflare/test/directory-split.test.js | 44 ++++++ .../test/fixtures/split/package.json | 9 ++ .../test/fixtures/split/src/middleware.ts | 10 ++ .../pages/[language]/files/[...path].astro | 37 +++++ .../split/src/pages/[person]/[car].astro | 14 ++ .../split/src/pages/blog/[post].astro | 14 ++ .../fixtures/split/src/pages/blog/cool.astro | 11 ++ .../split/src/pages/files/[...path].astro | 37 +++++ .../test/fixtures/split/src/pages/index.astro | 22 +++ .../fixtures/split/src/pages/prerender.astro | 14 ++ pnpm-lock.yaml | 9 ++ 16 files changed, 338 insertions(+), 46 deletions(-) create mode 100644 .changeset/healthy-books-study.md create mode 100644 packages/integrations/cloudflare/test/directory-split.test.js create mode 100644 packages/integrations/cloudflare/test/fixtures/split/package.json create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/middleware.ts create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/[language]/files/[...path].astro create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/[person]/[car].astro create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/[post].astro create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/cool.astro create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/files/[...path].astro create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/index.astro create mode 100644 packages/integrations/cloudflare/test/fixtures/split/src/pages/prerender.astro diff --git a/.changeset/healthy-books-study.md b/.changeset/healthy-books-study.md new file mode 100644 index 000000000000..e4b51b041d92 --- /dev/null +++ b/.changeset/healthy-books-study.md @@ -0,0 +1,7 @@ +--- +'@astrojs/cloudflare': minor +--- + +Split Support in Cloudflare + +Adds support for configuring `build.split` when using the Cloudflare adapter diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index 6f69e3bddf46..a6638f36dd16 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -48,7 +48,11 @@ Cloudflare Pages has 2 different modes for deploying functions, `advanced` mode For most projects the adapter default of `advanced` will be sufficient; the `dist` folder will contain your compiled project. Switching to directory mode allows you to use [pages plugins](https://developers.cloudflare.com/pages/platform/functions/plugins/) such as [Sentry](https://developers.cloudflare.com/pages/platform/functions/plugins/sentry/) or write custom code to enable logging. -In directory mode the adapter will compile the client side part of your app the same way, but moves the worker script into a `functions` folder in the project root. The adapter will only ever place a `[[path]].js` in that folder, allowing you to add additional plugins and pages middleware which can be checked into version control. Cloudflare documentation contains more information about [writing custom functions](https://developers.cloudflare.com/pages/platform/functions/). +In directory mode, the adapter will compile the client side part of your app the same way by default, but moves the worker script into a `functions` folder in the project root. In this case, the adapter will only ever place a `[[path]].js` in that folder, allowing you to add additional plugins and pages middleware which can be checked into version control. + +With the build configuration `split: true`, the adapter instead compiles a separate bundle for each page. This option requires some manual maintenance of the `functions` folder. Files emitted by Astro will overwrite existing `functions` files with identical names, so you must choose unique file names for each file you manually add. Additionally, the adapter will never empty the `functions` folder of outdated files, so you must clean up the folder manually when you remove pages. + +Note that this adapter does not support using [Cloudflare Pages Middleware](https://developers.cloudflare.com/pages/platform/functions/middleware/). Astro will bundle the [Astro middleware](https://docs.astro.build/en/guides/middleware/) into each page. ```ts // directory mode diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json index fe64ee166eb6..f4ad834a0ce4 100644 --- a/packages/integrations/cloudflare/package.json +++ b/packages/integrations/cloudflare/package.json @@ -35,7 +35,8 @@ "build": "astro-scripts build \"src/**/*.ts\" && tsc", "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", - "test": "mocha --exit --timeout 30000 test/" + "test": "mocha --exit --timeout 30000 test/", + "test:match": "mocha --exit --timeout 30000 -g" }, "dependencies": { "@astrojs/underscore-redirects": "^0.1.0", diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 252dd778a530..f4ee26feaa4f 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -1,8 +1,9 @@ import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects'; -import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro'; +import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro'; 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'; @@ -14,20 +15,21 @@ interface BuildConfig { server: URL; client: URL; serverEntry: string; + split?: boolean; } export function getAdapter(isModeDirectory: boolean): AstroAdapter { return isModeDirectory ? { - name: '@astrojs/cloudflare', - serverEntrypoint: '@astrojs/cloudflare/server.directory.js', - exports: ['onRequest'], - } + 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 = { @@ -41,6 +43,7 @@ export default function createIntegration(args?: Options): AstroIntegration { let _config: AstroConfig; let _buildConfig: BuildConfig; const isModeDirectory = args?.mode === 'directory'; + let _entryPoints = new Map(); return { name: '@astrojs/cloudflare', @@ -90,35 +93,99 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr.target = 'webworker'; } }, + 'astro:build:ssr': ({ manifest, entryPoints }) => { + _entryPoints = entryPoints; + }, 'astro:build:done': async ({ pages, routes, dir }) => { - const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)); - const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir); - const buildPath = fileURLToPath(entryUrl); - // A URL for the final build path after renaming - const finalBuildUrl = pathToFileURL(buildPath.replace(/\.mjs$/, '.js')); - - await esbuild.build({ - target: 'es2020', - platform: 'browser', - conditions: ['workerd', 'worker', 'browser'], - entryPoints: [entryPath], - outfile: buildPath, - allowOverwrite: true, - format: 'esm', - bundle: true, - minify: _config.vite?.build?.minify !== false, - banner: { - js: SHIM, - }, - logOverride: { - 'ignored-bare-import': 'silent', - }, - }); + const functionsUrl = new URL('functions/', _config.root); + + if (isModeDirectory) { + await fs.promises.mkdir(functionsUrl, { recursive: true }); + } + + if (isModeDirectory && _buildConfig.split) { + const entryPointsRouteData = [..._entryPoints.keys()] + const entryPointsURL = [..._entryPoints.values()] + const entryPaths = entryPointsURL.map((entry) => fileURLToPath(entry)); + const outputDir = fileURLToPath(new URL('.astro', _buildConfig.server)); + + // NOTE: AFAIK, esbuild keeps the order of the entryPoints array + const { outputFiles } = await esbuild.build({ + target: 'es2020', + platform: 'browser', + conditions: ['workerd', 'worker', 'browser'], + entryPoints: entryPaths, + outdir: outputDir, + allowOverwrite: true, + format: 'esm', + bundle: true, + minify: _config.vite?.build?.minify !== false, + banner: { + js: SHIM, + }, + logOverride: { + 'ignored-bare-import': 'silent', + }, + write: false, + }); - // Rename to worker.js - await fs.promises.rename(buildPath, finalBuildUrl); + // 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(/(\[\.\.\.)(\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); + } - // throw the server folder in the bin + } else { + const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)); + const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir); + const buildPath = fileURLToPath(entryUrl); + // A URL for the final build path after renaming + const finalBuildUrl = pathToFileURL(buildPath.replace(/\.mjs$/, '.js')); + + await esbuild.build({ + target: 'es2020', + platform: 'browser', + conditions: ['workerd', 'worker', 'browser'], + entryPoints: [entryPath], + outfile: buildPath, + allowOverwrite: true, + format: 'esm', + bundle: true, + minify: _config.vite?.build?.minify !== false, + banner: { + js: SHIM, + }, + logOverride: { + 'ignored-bare-import': 'silent', + }, + }); + + // Rename to worker.js + await fs.promises.rename(buildPath, finalBuildUrl); + + if (isModeDirectory) { + const directoryUrl = new URL('[[path]].js', functionsUrl); + await fs.promises.rename(finalBuildUrl, directoryUrl); + } + + } + + // // // throw the server folder in the bin const serverUrl = new URL(_buildConfig.server); await fs.promises.rm(serverUrl, { recursive: true, force: true }); @@ -225,14 +292,6 @@ export default function createIntegration(args?: Options): AstroIntegration { ) ); } - - if (isModeDirectory) { - const functionsUrl = new URL('functions/', _config.root); - await fs.promises.mkdir(functionsUrl, { recursive: true }); - - const directoryUrl = new URL('[[path]].js', functionsUrl); - await fs.promises.rename(finalBuildUrl, directoryUrl); - } }, }, }; diff --git a/packages/integrations/cloudflare/src/server.directory.ts b/packages/integrations/cloudflare/src/server.directory.ts index da23605573e3..f9f71a33b5b8 100644 --- a/packages/integrations/cloudflare/src/server.directory.ts +++ b/packages/integrations/cloudflare/src/server.directory.ts @@ -61,5 +61,5 @@ export function createExports(manifest: SSRManifest) { }); }; - return { onRequest }; + return { onRequest, manifest }; } diff --git a/packages/integrations/cloudflare/test/directory-split.test.js b/packages/integrations/cloudflare/test/directory-split.test.js new file mode 100644 index 000000000000..8bb6cd87286e --- /dev/null +++ b/packages/integrations/cloudflare/test/directory-split.test.js @@ -0,0 +1,44 @@ +import { loadFixture } from './test-utils.js'; +import { expect } from 'chai'; +import cloudflare from '../dist/index.js'; + +/** @type {import('./test-utils').Fixture} */ +describe('Cloudflare SSR split', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/split/', + adapter: cloudflare({ mode: 'directory' }), + output: "server", + build: { + split: true, + excludeMiddleware: false + }, + vite: { + build: { + minify: false, + }, + }, + }); + await fixture.build(); + }); + + after(() => { + fixture.clean(); + }); + + it('generates functions folders inside the project root, and checks that each page is emitted by astro', async () => { + expect(await fixture.pathExists('../functions')).to.be.true; + expect(await fixture.pathExists('../functions/index.js')).to.be.true; + expect(await fixture.pathExists('../functions/blog/cool.js')).to.be.true; + expect(await fixture.pathExists('../functions/blog/[post].js')).to.be.true; + 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; + }); + + it('generates pre-rendered files', async () => { + expect(await fixture.pathExists('./prerender/index.html')).to.be.true; + }); +}); diff --git a/packages/integrations/cloudflare/test/fixtures/split/package.json b/packages/integrations/cloudflare/test/fixtures/split/package.json new file mode 100644 index 000000000000..fd7dcc2530da --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/astro-cloudflare-split", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/cloudflare": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/middleware.ts b/packages/integrations/cloudflare/test/fixtures/split/src/middleware.ts new file mode 100644 index 000000000000..a6ce640cb348 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/middleware.ts @@ -0,0 +1,10 @@ +import { defineMiddleware } from "astro/middleware"; + +export const onRequest = defineMiddleware(({ locals, request }, next) => { + // intercept response data from a request + // optionally, transform the response by modifying `locals` + locals.title = "New title" + + // return a Response or the result of calling `next()` + return next() +}); diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/[language]/files/[...path].astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/[language]/files/[...path].astro new file mode 100644 index 000000000000..84ad532286a0 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/[language]/files/[...path].astro @@ -0,0 +1,37 @@ +--- +const files = [ + { + slug: undefined, + title: 'Root level', + }, + { + slug: 'test.png', + title: "One level" + }, + { + slug: 'assets/test.png', + title: "Two levels" + }, + { + slug: 'assets/images/test.png', + title: 'Three levels', + } +]; + +const { path } = Astro.params; +const page = files.find((page) => page.slug === path); +const { title } = page; + +--- + + +

Files / Rest Parameters / {title}

+

DEBUG: {path}

+

index

+ + + diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/[person]/[car].astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/[person]/[car].astro new file mode 100644 index 000000000000..f4fda9dc5336 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/[person]/[car].astro @@ -0,0 +1,14 @@ +--- +const { person, car } = Astro.params; +--- + + +

{person} / {car}

+

index

+ + + diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/[post].astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/[post].astro new file mode 100644 index 000000000000..7b0e1e5b8a8d --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/[post].astro @@ -0,0 +1,14 @@ +--- +const { post } = Astro.params; +--- + + +

Blog / {post}

+

index

+ + + diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/cool.astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/cool.astro new file mode 100644 index 000000000000..7127282a4585 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/blog/cool.astro @@ -0,0 +1,11 @@ + + +

Blog / Cool

+

index

+ + + diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/files/[...path].astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/files/[...path].astro new file mode 100644 index 000000000000..84ad532286a0 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/files/[...path].astro @@ -0,0 +1,37 @@ +--- +const files = [ + { + slug: undefined, + title: 'Root level', + }, + { + slug: 'test.png', + title: "One level" + }, + { + slug: 'assets/test.png', + title: "Two levels" + }, + { + slug: 'assets/images/test.png', + title: 'Three levels', + } +]; + +const { path } = Astro.params; +const page = files.find((page) => page.slug === path); +const { title } = page; + +--- + + +

Files / Rest Parameters / {title}

+

DEBUG: {path}

+

index

+ + + diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/index.astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/index.astro new file mode 100644 index 000000000000..a7f5640469e2 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/index.astro @@ -0,0 +1,22 @@ +--- +const data = Astro.locals; +--- + + +

Index

+

Middleware ({data.title})

+

prerender

+

sub-route

+

dynamic route in static sub-route

+

dynamic route in dynamic sub-route

+

rest parameters root level

+

rest parameters one level

+

rest parameters two level

+

rest parameters three level

+ + + diff --git a/packages/integrations/cloudflare/test/fixtures/split/src/pages/prerender.astro b/packages/integrations/cloudflare/test/fixtures/split/src/pages/prerender.astro new file mode 100644 index 000000000000..bdda9b12ceab --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/split/src/pages/prerender.astro @@ -0,0 +1,14 @@ +--- +export const prerender = true; +--- + + +

Prerender

+

index

+ + + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5649d326d5c..ee0f3c0911e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3675,6 +3675,15 @@ importers: specifier: workspace:* version: link:../../../../../astro + packages/integrations/cloudflare/test/fixtures/split: + dependencies: + '@astrojs/cloudflare': + specifier: workspace:* + version: link:../../.. + astro: + specifier: workspace:* + version: link:../../../../../astro + packages/integrations/cloudflare/test/fixtures/with-solid-js: dependencies: '@astrojs/cloudflare': From 5b1745bf31ef2523f769be7d925899bd237de11b Mon Sep 17 00:00:00 2001 From: ematipico Date: Fri, 30 Jun 2023 09:12:30 +0000 Subject: [PATCH 2/3] [ci] format --- packages/integrations/cloudflare/src/index.ts | 24 +++++++++---------- .../cloudflare/test/directory-split.test.js | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index f4ee26feaa4f..298d798fb884 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 = { @@ -104,8 +104,8 @@ export default function createIntegration(args?: Options): AstroIntegration { } if (isModeDirectory && _buildConfig.split) { - const entryPointsRouteData = [..._entryPoints.keys()] - const entryPointsURL = [..._entryPoints.values()] + const entryPointsRouteData = [..._entryPoints.keys()]; + const entryPointsURL = [..._entryPoints.values()]; const entryPaths = entryPointsURL.map((entry) => fileURLToPath(entry)); const outputDir = fileURLToPath(new URL('.astro', _buildConfig.server)); @@ -142,14 +142,13 @@ export default function createIntegration(args?: Options): AstroIntegration { return `[[${p2}]]`; }); - const fileUrl = new URL(fileName, functionsUrl) + 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); } - } else { const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)); const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir); @@ -182,7 +181,6 @@ export default function createIntegration(args?: Options): AstroIntegration { const directoryUrl = new URL('[[path]].js', functionsUrl); await fs.promises.rename(finalBuildUrl, directoryUrl); } - } // // // throw the server folder in the bin diff --git a/packages/integrations/cloudflare/test/directory-split.test.js b/packages/integrations/cloudflare/test/directory-split.test.js index 8bb6cd87286e..384543a4b896 100644 --- a/packages/integrations/cloudflare/test/directory-split.test.js +++ b/packages/integrations/cloudflare/test/directory-split.test.js @@ -10,10 +10,10 @@ describe('Cloudflare SSR split', () => { fixture = await loadFixture({ root: './fixtures/split/', adapter: cloudflare({ mode: 'directory' }), - output: "server", + output: 'server', build: { split: true, - excludeMiddleware: false + excludeMiddleware: false, }, vite: { build: { From fcba0f0199b53e2077aae36b4ea2b287482d5c5c Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 30 Jun 2023 17:28:17 +0800 Subject: [PATCH 3/3] Refactor telemetry usage flow (#7522) --- packages/astro/src/@types/astro.ts | 1 - packages/astro/src/cli/index.ts | 8 +++----- packages/astro/src/cli/telemetry.ts | 6 +++--- packages/astro/src/core/add/index.ts | 4 +--- packages/astro/src/core/build/index.ts | 2 -- packages/astro/src/core/config/settings.ts | 1 - packages/astro/src/core/dev/container.ts | 8 +------- packages/astro/src/core/dev/dev.ts | 5 ++--- packages/astro/src/core/preview/index.ts | 2 -- .../astro/src/core/render/dev/environment.ts | 1 - packages/astro/src/core/render/environment.ts | 2 -- .../src/vite-plugin-astro-server/request.ts | 4 +--- packages/astro/test/test-utils.js | 20 ++++++------------- .../test/units/routing/route-matching.test.js | 1 - packages/astro/test/units/shiki/shiki.test.js | 2 +- 15 files changed, 18 insertions(+), 49 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index c544fd8b8c84..ef958d2a89e1 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1375,7 +1375,6 @@ export interface AstroSettings { tsConfig: TsConfigJson | undefined; tsConfigPath: string | undefined; watchFiles: string[]; - forceDisableTelemetry: boolean; timer: AstroTimer; } diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 250729dd9b80..4f0fa941cd94 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -148,7 +148,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { telemetry.record(event.eventCliSession(cmd)); const packages = flags._.slice(3) as string[]; - return await add(packages, { cwd: root, flags, logging, telemetry }); + return await add(packages, { cwd: root, flags, logging }); } case 'docs': { telemetry.record(event.eventCliSession(cmd)); @@ -170,7 +170,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { // Do not track session start, since the user may be trying to enable, // disable, or modify telemetry settings. const subcommand = flags._[3]?.toString(); - return await telemetryHandler.update(subcommand, { flags, telemetry }); + return await telemetryHandler.update(subcommand, { flags }); } } @@ -209,7 +209,6 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { configFlagPath, flags, logging, - telemetry, handleConfigError(e) { handleConfigError(e, { cmd, cwd: root, flags, logging }); info(logging, 'astro', 'Continuing with previous valid configuration\n'); @@ -224,7 +223,6 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { return await build(settings, { flags, logging, - telemetry, teardownCompiler: true, mode: flags.mode, }); @@ -256,7 +254,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { case 'preview': { const { default: preview } = await import('../core/preview/index.js'); - const server = await preview(settings, { logging, telemetry, flags }); + const server = await preview(settings, { logging, flags }); if (server) { return await server.closed(); // keep alive until the server is closed } diff --git a/packages/astro/src/cli/telemetry.ts b/packages/astro/src/cli/telemetry.ts index a8ec1c174f7c..7a5bfd469c7b 100644 --- a/packages/astro/src/cli/telemetry.ts +++ b/packages/astro/src/cli/telemetry.ts @@ -1,13 +1,13 @@ /* eslint-disable no-console */ -import type { AstroTelemetry } from '@astrojs/telemetry'; import type yargs from 'yargs-parser'; import * as msg from '../core/messages.js'; +import { telemetry } from '../events/index.js'; + export interface TelemetryOptions { flags: yargs.Arguments; - telemetry: AstroTelemetry; } -export async function update(subcommand: string, { flags, telemetry }: TelemetryOptions) { +export async function update(subcommand: string, { flags }: TelemetryOptions) { const isValid = ['enable', 'disable', 'reset'].includes(subcommand); if (flags.help || flags.h || !isValid) { diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index 7af09b4de3a2..1ce9163bf00f 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/core/add/index.ts @@ -1,4 +1,3 @@ -import type { AstroTelemetry } from '@astrojs/telemetry'; import boxen from 'boxen'; import { diffWords } from 'diff'; import { execa } from 'execa'; @@ -30,7 +29,6 @@ import { wrapDefaultExport } from './wrapper.js'; export interface AddOptions { logging: LogOptions; flags: yargs.Arguments; - telemetry: AstroTelemetry; cwd?: string; } @@ -83,7 +81,7 @@ async function getRegistry(): Promise { return stdout || 'https://registry.npmjs.org'; } -export default async function add(names: string[], { cwd, flags, logging, telemetry }: AddOptions) { +export default async function add(names: string[], { cwd, flags, logging }: AddOptions) { applyPolyfill(); if (flags.help || names.length === 0) { printHelp({ diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index ca1c98aff29a..037c462fde1e 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -1,4 +1,3 @@ -import type { AstroTelemetry } from '@astrojs/telemetry'; import type { AstroConfig, AstroSettings, ManifestData, RuntimeMode } from '../../@types/astro'; import fs from 'fs'; @@ -26,7 +25,6 @@ import { getTimeStat } from './util.js'; export interface BuildOptions { mode?: RuntimeMode; logging: LogOptions; - telemetry: AstroTelemetry; /** * Teardown the compiler WASM instance after build. This can improve performance when * building once, but may cause a performance hit if building multiple times in a row. diff --git a/packages/astro/src/core/config/settings.ts b/packages/astro/src/core/config/settings.ts index c5e9def8ce43..8040b3fbe46f 100644 --- a/packages/astro/src/core/config/settings.ts +++ b/packages/astro/src/core/config/settings.ts @@ -104,7 +104,6 @@ export function createBaseSettings(config: AstroConfig): AstroSettings { scripts: [], clientDirectives: getDefaultClientDirectives(), watchFiles: [], - forceDisableTelemetry: false, timer: new AstroTimer(), }; } diff --git a/packages/astro/src/core/dev/container.ts b/packages/astro/src/core/dev/container.ts index b65a2c48bbb6..4a1d6cc6b252 100644 --- a/packages/astro/src/core/dev/container.ts +++ b/packages/astro/src/core/dev/container.ts @@ -46,7 +46,6 @@ export interface CreateContainerParams { // The string passed to --config and the resolved path configFlag?: string; configFlagPath?: string; - disableTelemetry?: boolean; } export async function createContainer(params: CreateContainerParams = {}): Promise { @@ -55,13 +54,8 @@ export async function createContainer(params: CreateContainerParams = {}): Promi logging = defaultLogging, settings = await createDefaultDevSettings(params.userConfig, params.root), fs = nodeFs, - disableTelemetry, } = params; - if (disableTelemetry) { - settings.forceDisableTelemetry = true; - } - // Initialize applyPolyfill(); settings = await runHookConfigSetup({ @@ -149,7 +143,7 @@ export async function runInContainer( params: CreateContainerParams, callback: (container: Container) => Promise | void ) { - const container = await createContainer({ ...params, disableTelemetry: true }); + const container = await createContainer(params); try { await callback(container); } finally { diff --git a/packages/astro/src/core/dev/dev.ts b/packages/astro/src/core/dev/dev.ts index 8dbaabddf884..5308eaf6b3ca 100644 --- a/packages/astro/src/core/dev/dev.ts +++ b/packages/astro/src/core/dev/dev.ts @@ -1,4 +1,3 @@ -import type { AstroTelemetry } from '@astrojs/telemetry'; import type http from 'http'; import { cyan } from 'kleur/colors'; import type { AddressInfo } from 'net'; @@ -7,6 +6,7 @@ import type * as vite from 'vite'; import type yargs from 'yargs-parser'; import type { AstroSettings } from '../../@types/astro'; import { attachContentServerListeners } from '../../content/index.js'; +import { telemetry } from '../../events/index.js'; import { info, warn, type LogOptions } from '../logger/core.js'; import * as msg from '../messages.js'; import { printHelp } from '../messages.js'; @@ -18,7 +18,6 @@ export interface DevOptions { configFlagPath: string | undefined; flags?: yargs.Arguments; logging: LogOptions; - telemetry: AstroTelemetry; handleConfigError: (error: Error) => void; isRestart?: boolean; } @@ -56,7 +55,7 @@ export default async function dev( } const devStart = performance.now(); - await options.telemetry.record([]); + await telemetry.record([]); // Create a container which sets up the Vite server. const restart = await createContainerWithAutomaticRestart({ diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index faaa67428ab0..492afe03f427 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -1,4 +1,3 @@ -import type { AstroTelemetry } from '@astrojs/telemetry'; import { cyan } from 'kleur/colors'; import { createRequire } from 'module'; import { pathToFileURL } from 'url'; @@ -12,7 +11,6 @@ import { getResolvedHostForHttpServer } from './util.js'; interface PreviewOptions { logging: LogOptions; - telemetry: AstroTelemetry; flags?: Arguments; } diff --git a/packages/astro/src/core/render/dev/environment.ts b/packages/astro/src/core/render/dev/environment.ts index ea11ee99959c..ec7cd4534d13 100644 --- a/packages/astro/src/core/render/dev/environment.ts +++ b/packages/astro/src/core/render/dev/environment.ts @@ -32,7 +32,6 @@ export function createDevelopmentEnvironment( site: settings.config.site, ssr: isServerLikeOutput(settings.config), streaming: true, - telemetry: Boolean(settings.forceDisableTelemetry), }); return { diff --git a/packages/astro/src/core/render/environment.ts b/packages/astro/src/core/render/environment.ts index a141ff728a51..7cd1ae3d8799 100644 --- a/packages/astro/src/core/render/environment.ts +++ b/packages/astro/src/core/render/environment.ts @@ -23,7 +23,6 @@ export interface Environment { site?: string; ssr: boolean; streaming: boolean; - telemetry?: boolean; } export type CreateEnvironmentArgs = Environment; @@ -53,6 +52,5 @@ export function createBasicEnvironment(options: CreateBasicEnvironmentArgs): Env routeCache: new RouteCache(options.logging, mode), ssr: options.ssr ?? true, streaming: options.streaming ?? true, - telemetry: false, }); } diff --git a/packages/astro/src/vite-plugin-astro-server/request.ts b/packages/astro/src/vite-plugin-astro-server/request.ts index 3c5da0575b67..197033b048c9 100644 --- a/packages/astro/src/vite-plugin-astro-server/request.ts +++ b/packages/astro/src/vite-plugin-astro-server/request.ts @@ -91,9 +91,7 @@ export async function handleRequest( // Our error should already be complete, but let's try to add a bit more through some guesswork const errorWithMetadata = collectErrorMetadata(err, config.root); - if (env.telemetry !== false) { - telemetry.record(eventError({ cmd: 'dev', err: errorWithMetadata, isFatal: false })); - } + telemetry.record(eventError({ cmd: 'dev', err: errorWithMetadata, isFatal: false })); error(env.logging, null, msg.formatErrorMessage(errorWithMetadata)); handle500Response(moduleLoader, res, errorWithMetadata); diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index 583a6c61e4d9..953ebf6daeeb 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -22,6 +22,9 @@ polyfill(globalThis, { exclude: 'window document', }); +// Disable telemetry when running tests +process.env.ASTRO_TELEMETRY_DISABLED = true; + /** * @typedef {import('undici').Response} Response * @typedef {import('../src/core/dev/dev').DedvServer} DevServer @@ -146,13 +149,6 @@ export async function loadFixture(inlineConfig) { return settings; }; - /** @type {import('@astrojs/telemetry').AstroTelemetry} */ - const telemetry = { - record() { - return Promise.resolve(); - }, - }; - const resolveUrl = (url) => `http://${config.server.host || 'localhost'}:${config.server.port}${url.replace(/^\/?/, '/')}`; @@ -184,7 +180,7 @@ export async function loadFixture(inlineConfig) { return { build: async (opts = {}) => { process.env.NODE_ENV = 'production'; - return build(await getSettings(), { logging, telemetry, ...opts }); + return build(await getSettings(), { logging, ...opts }); }, sync: async (opts) => sync(await getSettings(), { logging, fs, ...opts }), check: async (opts) => { @@ -192,7 +188,7 @@ export async function loadFixture(inlineConfig) { }, startDevServer: async (opts = {}) => { process.env.NODE_ENV = 'development'; - devServer = await dev(await getSettings(), { logging, telemetry, ...opts }); + devServer = await dev(await getSettings(), { logging, ...opts }); config.server.host = parseAddressToHost(devServer.address.address); // update host config.server.port = devServer.address.port; // update port return devServer; @@ -214,11 +210,7 @@ export async function loadFixture(inlineConfig) { }, preview: async (opts = {}) => { process.env.NODE_ENV = 'production'; - const previewServer = await preview(await getSettings(), { - logging, - telemetry, - ...opts, - }); + const previewServer = await preview(await getSettings(), { logging, ...opts }); config.server.host = parseAddressToHost(previewServer.host); // update host config.server.port = previewServer.port; // update port return previewServer; diff --git a/packages/astro/test/units/routing/route-matching.test.js b/packages/astro/test/units/routing/route-matching.test.js index 46536176533f..fca2064ea0e4 100644 --- a/packages/astro/test/units/routing/route-matching.test.js +++ b/packages/astro/test/units/routing/route-matching.test.js @@ -135,7 +135,6 @@ describe('Route matching', () => { output: 'hybrid', adapter: testAdapter(), }, - disableTelemetry: true, }); settings = container.settings; diff --git a/packages/astro/test/units/shiki/shiki.test.js b/packages/astro/test/units/shiki/shiki.test.js index 21f93dce819b..0d67dda35f0a 100644 --- a/packages/astro/test/units/shiki/shiki.test.js +++ b/packages/astro/test/units/shiki/shiki.test.js @@ -9,7 +9,7 @@ describe('', () => { let container; let mod; before(async () => { - container = await createContainer({ root, disableTelemetry: true }); + container = await createContainer({ root }); const loader = createViteLoader(container.viteServer); mod = await loader.import('astro/components/Shiki.js'); });