-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): Support for off-main-thread in develop (#35648)
* feat(gatsby): initial support for off-main-thread in develop Co-authored-by: tyhopp <[email protected]> * feat(gatsby): Refactor partytown local proxy logic * Use origin instead of host in serve/develop proxy Co-authored-by: Michal Piechowiak <[email protected]>
- Loading branch information
Showing
5 changed files
with
105 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/gatsby/src/internal-plugins/partytown/gatsby-browser.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { ReactElement, useState } from "react" | ||
import type { GatsbySSR } from "gatsby" | ||
import { Partytown } from "@builder.io/partytown/react" | ||
import { PartytownContext } from "gatsby-script" | ||
import type { PartytownProps } from "@builder.io/partytown/react" | ||
|
||
interface ICollectedForwardsState { | ||
collectedForwards: Set<string> | ||
collectedAnyScript: boolean | ||
} | ||
|
||
function PartytownProvider({ children }): ReactElement { | ||
const [{ collectedForwards, collectedAnyScript }, setState] = | ||
useState<ICollectedForwardsState>({ | ||
collectedForwards: new Set(), | ||
collectedAnyScript: false, | ||
}) | ||
|
||
return ( | ||
<PartytownContext.Provider | ||
value={{ | ||
collectScript: (newScript: PartytownProps): void => { | ||
let stateShouldChange = false | ||
const potentialNewState = { | ||
collectedAnyScript, | ||
collectedForwards, | ||
} | ||
|
||
if (!collectedAnyScript) { | ||
potentialNewState.collectedAnyScript = true | ||
stateShouldChange = true | ||
} | ||
|
||
if (newScript?.forward) { | ||
if (Array.isArray(newScript.forward)) { | ||
for (const singleForward of newScript.forward) { | ||
if (!potentialNewState.collectedForwards.has(singleForward)) { | ||
potentialNewState.collectedForwards.add(singleForward) | ||
stateShouldChange = true | ||
} | ||
} | ||
} else { | ||
console.log(`unexpected shape of forward`, newScript) | ||
} | ||
} | ||
|
||
if (stateShouldChange) { | ||
setState(potentialNewState) | ||
} | ||
}, | ||
}} | ||
> | ||
{children} | ||
{collectedAnyScript && ( | ||
<Partytown key="partytown" forward={Array.from(collectedForwards)} /> | ||
)} | ||
</PartytownContext.Provider> | ||
) | ||
} | ||
|
||
export const wrapRootElement: GatsbySSR[`wrapRootElement`] = ({ element }) => ( | ||
<PartytownProvider>{element}</PartytownProvider> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import proxy from "express-http-proxy" | ||
import type { RequestHandler } from "express" | ||
|
||
export const partytownProxyPath = `/__partytown-proxy` | ||
|
||
export function partytownProxy( | ||
partytownProxiedURLs: Array<string> | ||
): RequestHandler { | ||
return proxy(req => new URL(req.query.url as string).origin as string, { | ||
filter: req => partytownProxiedURLs.some(url => req.query?.url === url), | ||
proxyReqPathResolver: req => { | ||
const { pathname = ``, search = `` } = new URL(req.query?.url as string) | ||
return pathname + search | ||
}, | ||
}) | ||
} |