Skip to content

Commit

Permalink
feat(gatsby): Refactor partytown local proxy logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tyhopp committed May 13, 2022
1 parent d22faab commit 87f4294
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
16 changes: 5 additions & 11 deletions packages/gatsby/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import { initTracer } from "../utils/tracer"
import { configureTrailingSlash } from "../utils/express-middlewares"
import { getDataStore, detectLmdbStore } from "../datastore"
import { functionMiddlewares } from "../internal-plugins/functions/middleware"
import proxy from "express-http-proxy"
import {
partytownProxyPath,
partytownProxy,
} from "../internal-plugins/partytown/proxy"

process.env.GATSBY_EXPERIMENTAL_LMDB_STORE = `1`
detectLmdbStore()
Expand Down Expand Up @@ -124,16 +127,7 @@ module.exports = async (program: IServeProgram): Promise<void> => {
// Proxy gatsby-script using off-main-thread strategy
const { partytownProxiedURLs = [] } = config || {}

app.use(
`/__partytown-proxy`,
proxy(req => new URL(req.query.url as string).host 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
},
})
)
app.use(partytownProxyPath, partytownProxy(partytownProxiedURLs))

// eslint-disable-next-line new-cap
const router = express.Router()
Expand Down
23 changes: 14 additions & 9 deletions packages/gatsby/src/internal-plugins/partytown/gatsby-browser.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import React, { useState } from "react"
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"

// TODO: type return and props
// eslint-disable-next-line
function PartytownProvider({ children }) {
const [{ collectedForwards, collectedAnyScript }, setState] = useState<{
collectedForwards: Set<string>
collectedAnyScript: boolean
}>({ collectedForwards: new Set(), collectedAnyScript: false })
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={{
Expand All @@ -26,7 +31,7 @@ function PartytownProvider({ children }) {
stateShouldChange = true
}

if (newScript.forward) {
if (newScript?.forward) {
if (Array.isArray(newScript.forward)) {
for (const singleForward of newScript.forward) {
if (!potentialNewState.collectedForwards.has(singleForward)) {
Expand Down
15 changes: 3 additions & 12 deletions packages/gatsby/src/internal-plugins/partytown/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path"
import { copyLibFiles } from "@builder.io/partytown/utils"
import proxy from "express-http-proxy"
import { CreateDevServerArgs } from "gatsby"
import { partytownProxyPath, partytownProxy } from "./proxy"

/**
* Copy Partytown library files to public.
Expand All @@ -26,7 +26,7 @@ exports.createPages = ({ actions, store }): void => {
const encodedURL: string = encodeURI(host)

createRedirect({
fromPath: `/__partytown-proxy?url=${encodedURL}`,
fromPath: `${partytownProxyPath}?url=${encodedURL}`,
toPath: encodedURL,
statusCode: 200,
})
Expand All @@ -40,14 +40,5 @@ export async function onCreateDevServer({
const { config } = store.getState()
const { partytownProxiedURLs = [] } = config || {}

app.use(
`/__partytown-proxy`,
proxy(req => new URL(req.query.url as string).host 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
},
})
)
app.use(partytownProxyPath, partytownProxy(partytownProxiedURLs))
}
16 changes: 16 additions & 0 deletions packages/gatsby/src/internal-plugins/partytown/proxy.ts
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).host 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
},
})
}

0 comments on commit 87f4294

Please sign in to comment.