From b36ecb717e2cb623501c6d9a60471ac4daba43a8 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 23 Jun 2022 09:03:43 -0400 Subject: [PATCH] Include partytown scripts in SSR manifest (#3686) * Include partytown scripts in SSR manifst * Adds a changeset --- .changeset/cuddly-llamas-arrive.md | 5 +++ .../fixtures/ssr-partytown/astro.config.mjs | 7 ++++ .../test/fixtures/ssr-partytown/package.json | 9 +++++ .../ssr-partytown/src/pages/index.astro | 6 ++++ packages/astro/test/ssr-partytown.test.js | 34 +++++++++++++++++++ packages/integrations/partytown/src/index.ts | 11 +++++- pnpm-lock.yaml | 17 +++++----- 7 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 .changeset/cuddly-llamas-arrive.md create mode 100644 packages/astro/test/fixtures/ssr-partytown/astro.config.mjs create mode 100644 packages/astro/test/fixtures/ssr-partytown/package.json create mode 100644 packages/astro/test/fixtures/ssr-partytown/src/pages/index.astro create mode 100644 packages/astro/test/ssr-partytown.test.js diff --git a/.changeset/cuddly-llamas-arrive.md b/.changeset/cuddly-llamas-arrive.md new file mode 100644 index 000000000000..40942b606c34 --- /dev/null +++ b/.changeset/cuddly-llamas-arrive.md @@ -0,0 +1,5 @@ +--- +'@astrojs/partytown': patch +--- + +Include partytown scripts in SSR manifest diff --git a/packages/astro/test/fixtures/ssr-partytown/astro.config.mjs b/packages/astro/test/fixtures/ssr-partytown/astro.config.mjs new file mode 100644 index 000000000000..ea1fb12db89c --- /dev/null +++ b/packages/astro/test/fixtures/ssr-partytown/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import partytown from '@astrojs/partytown'; + +// https://astro.build/config +export default defineConfig({ + integrations: [partytown()], +}); diff --git a/packages/astro/test/fixtures/ssr-partytown/package.json b/packages/astro/test/fixtures/ssr-partytown/package.json new file mode 100644 index 000000000000..5713e2d1bc46 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-partytown/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/ssr-partytown", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/partytown": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-partytown/src/pages/index.astro b/packages/astro/test/fixtures/ssr-partytown/src/pages/index.astro new file mode 100644 index 000000000000..743a9d35afbe --- /dev/null +++ b/packages/astro/test/fixtures/ssr-partytown/src/pages/index.astro @@ -0,0 +1,6 @@ + + testing + +

testing

+ + diff --git a/packages/astro/test/ssr-partytown.test.js b/packages/astro/test/ssr-partytown.test.js new file mode 100644 index 000000000000..5ea2bcde59e9 --- /dev/null +++ b/packages/astro/test/ssr-partytown.test.js @@ -0,0 +1,34 @@ +import { expect } from 'chai'; +import { load as cheerioLoad } from 'cheerio'; +import { loadFixture } from './test-utils.js'; +import testAdapter from './test-adapter.js'; + +describe('Using the Partytown integration in SSR', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-partytown/', + adapter: testAdapter(), + experimental: { + ssr: true, + }, + }); + await fixture.build(); + }); + + it('Has the scripts in the page', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/'); + const response = await app.render(request); + const html = await response.text(); + const $ = cheerioLoad(html); + expect($('script')).to.have.a.lengthOf(1); + }); + + it('The partytown scripts are in the manifest', async () => { + const app = await fixture.loadTestAdapterApp(); + expect(app.manifest.assets).to.contain('/~partytown/partytown-sw.js'); + }); +}); diff --git a/packages/integrations/partytown/src/index.ts b/packages/integrations/partytown/src/index.ts index 3d1101f968dd..51343b1691e3 100644 --- a/packages/integrations/partytown/src/index.ts +++ b/packages/integrations/partytown/src/index.ts @@ -1,8 +1,9 @@ import { partytownSnippet } from '@builder.io/partytown/integration'; -import { copyLibFiles } from '@builder.io/partytown/utils'; +import { copyLibFiles, libDirPath } from '@builder.io/partytown/utils'; import type { AstroConfig, AstroIntegration } from 'astro'; import { createRequire } from 'module'; import path from 'path'; +import * as fs from 'fs'; import { fileURLToPath } from 'url'; import sirv from './sirv.js'; const resolve = createRequire(import.meta.url).resolve; @@ -50,6 +51,14 @@ export default function createPlugin(options: PartytownOptions): AstroIntegratio debugDir: false, }); }, + 'astro:build:ssr': async ({ manifest }) => { + const dirpath = libDirPath({ debugDir: false }); + const files = await fs.promises.readdir(dirpath); + for(const file of files) { + if(file === 'debug') continue; + manifest.assets.push(`/~partytown/${file}`) + } + } }, }; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5690fe4e308..60a6af258cdc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1601,6 +1601,14 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/ssr-partytown: + specifiers: + '@astrojs/partytown': workspace:* + astro: workspace:* + dependencies: + '@astrojs/partytown': link:../../../../integrations/partytown + astro: link:../../.. + packages/astro/test/fixtures/ssr-scripts: specifiers: '@astrojs/preact': 'workspace:' @@ -8432,11 +8440,6 @@ packages: /debug/3.2.7: resolution: {integrity: sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==} - peerDependencies: - supports-color: '*' - peerDependenciesMeta: - supports-color: - optional: true dependencies: ms: 2.1.3 dev: false @@ -11358,8 +11361,6 @@ packages: debug: 3.2.7 iconv-lite: 0.4.24 sax: 1.2.4 - transitivePeerDependencies: - - supports-color dev: false /netmask/2.0.2: @@ -11443,8 +11444,6 @@ packages: rimraf: 2.7.1 semver: 5.7.1 tar: 4.4.19 - transitivePeerDependencies: - - supports-color dev: false /node-releases/2.0.5: