Skip to content

Commit

Permalink
cross-domain cookies just don't work a lot of the time, switch to pro…
Browse files Browse the repository at this point in the history
…xied worker
  • Loading branch information
rmarscher committed Jan 6, 2024
1 parent 5e52741 commit 15ac484
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
11 changes: 11 additions & 0 deletions apps/next/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ module.exports = function () {
compiler: {
removeConsole: disableBrowserLogs,
},
async rewrites() {
return [
{
source: '/worker/:path*',
destination: process.env.NEXT_PUBLIC_API_URL
? `${process.env.NEXT_PUBLIC_API_URL}/worker/:path*`
: 'http://localhost:8787/worker/:path*',
},
]
},

}

for (const plugin of plugins) {
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const getAllowedOriginHost = (app_url: string, request?: Request) => {
}

export const isCrossDomain = (appUrl?: string, apiUrl?: string) => {
return false;
// cross-domain cookies hardly work anymore and are going away
// you need to proxy or serve the worker on a subdomain
if (!appUrl || !apiUrl) return true
const appHost = new URL(appUrl).host
const apiHost = new URL(apiUrl).host
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type Bindings = Env & {
[k: string]: unknown
}

const app = new Hono<{ Bindings: Bindings }>()
const app = new Hono<{ Bindings: Bindings }>().basePath('/worker')

const corsHandler = async (c: Context<{ Bindings: Bindings }>, next: Next) => {
if (c.env.APP_URL === undefined) {
Expand All @@ -47,6 +47,7 @@ app.use('/trpc/*', corsHandler)
// Setup TRPC server with context
app.use('/trpc/*', async (c, next) => {
return await trpcServer({
endpoint: '/worker/trpc',
router: appRouter,
createContext: async ({ resHeaders }) => {
return await createContext(c.env, c, resHeaders)
Expand Down
2 changes: 1 addition & 1 deletion packages/app/utils/trpc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const TRPCProvider: React.FC<{
'x-enable-tokens': 'true',
}
},
url: `${getApiUrl()}/trpc`,
url: `${getApiUrl()}/worker/trpc`,
}),
],
})
Expand Down
2 changes: 1 addition & 1 deletion packages/app/utils/trpc/index.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function TRPCProvider(props: { children: React.ReactNode }) {
// always try to include cookies
})
},
url: `${process.env.NEXT_PUBLIC_API_URL}/trpc`,
url: `${typeof window !== 'undefined' ? '' : process.env.NEXT_PUBLIC_API_URL}/worker/trpc`,
}),
],
})
Expand Down

1 comment on commit 15ac484

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔ EAS production build completed

  • 🤖 Android build failed ❌
  • 🍏 IOS build failed ❌
Android QR IOS QR

Please sign in to comment.