Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include partytown scripts in SSR manifest #3686

Merged
merged 2 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.