Skip to content

Commit

Permalink
Include partytown scripts in SSR manifest (#3686)
Browse files Browse the repository at this point in the history
* Include partytown scripts in SSR manifst

* Adds a changeset
  • Loading branch information
matthewp authored Jun 23, 2022
1 parent aa883a5 commit b36ecb7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-llamas-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/partytown': patch
---

Include partytown scripts in SSR manifest
7 changes: 7 additions & 0 deletions packages/astro/test/fixtures/ssr-partytown/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import partytown from '@astrojs/partytown';

// https://astro.build/config
export default defineConfig({
integrations: [partytown()],
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/ssr-partytown/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/ssr-partytown",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/partytown": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head><title>testing</title></head>
<body>
<h1>testing</h1>
</body>
</html>
34 changes: 34 additions & 0 deletions packages/astro/test/ssr-partytown.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
11 changes: 10 additions & 1 deletion packages/integrations/partytown/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}`)
}
}
},
};
}
17 changes: 8 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b36ecb7

Please sign in to comment.