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

Add options to partytown integration #3380

Merged
merged 2 commits into from
May 16, 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/silent-books-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/partytown': patch
---

Add config options for integration
32 changes: 32 additions & 0 deletions packages/integrations/partytown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ Partytown should be ready-to-use with zero config. If you have an existing 3rd p

If you open the "Network" tab from [your browser's dev tools](https://developer.chrome.com/docs/devtools/open/), you should see the `partytown` proxy intercepting this request.

## Configuration

### config.debug

You can set debug mode using this integration's `config.debug` option. If `config.debug` is unset, it will fall back to `true` if the command is `dev`.

```js
// astro.config.mjs
export default {
integrations: [partytown({
// Example: Disable debug mode.
config: { debug: false },
})],
}
```

### config.forward

Because we’re moving third-party scripts to a web worker, the main thread needs to know which variables to patch on window, and when these services are called, the data is correctly forwarded to the web worker. You can to set it on the `config.forward` option.

```js
// astro.config.mjs
export default {
integrations: [partytown({
// Example: Add dataLayer.push as a forwarding-event.
config: { forward: ["dataLayer.push"] },
})],
}
```

## Read more

[Head to the Partytown docs](https://partytown.builder.io/configuration) for configuration options and more usage examples. You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.

[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
Expand Down
13 changes: 11 additions & 2 deletions packages/integrations/partytown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { createRequire } from 'module';
import path from 'path';
const resolve = createRequire(import.meta.url).resolve;

export default function createPlugin(): AstroIntegration {
type PartytownOptions = {
config?: {
forward?: string[];
debug?: boolean;
}
} | undefined;

export default function createPlugin(options: PartytownOptions): AstroIntegration {
let config: AstroConfig;
let partytownSnippetHtml: string;
const partytownEntrypoint = resolve('@builder.io/partytown/package.json');
Expand All @@ -16,7 +23,9 @@ export default function createPlugin(): AstroIntegration {
name: '@astrojs/partytown',
hooks: {
'astro:config:setup': ({ config: _config, command, injectScript }) => {
partytownSnippetHtml = partytownSnippet({ debug: command === 'dev' });
const forward = options?.config?.forward || []
const debug = options?.config?.debug || command === 'dev'
partytownSnippetHtml = partytownSnippet({ debug, forward });
injectScript('head-inline', partytownSnippetHtml);
},
'astro:config:done': ({ config: _config }) => {
Expand Down