diff --git a/biome.json b/biome.json index ed61f58d..d97fdb7e 100644 --- a/biome.json +++ b/biome.json @@ -3,7 +3,9 @@ "files": { "ignore": [ "coverage", + ".next", "node_modules", + "src/_lib", "src/vendor", "tsconfig.json", "tsconfig.*.json" diff --git a/examples/_dev/src/index.tsx b/examples/_dev/src/index.tsx index 9444496f..569a7072 100644 --- a/examples/_dev/src/index.tsx +++ b/examples/_dev/src/index.tsx @@ -3,7 +3,7 @@ import { Button, Farc, TextInput } from 'farc' import { app as todoApp } from './todos.js' export const app = new Farc({ - browserLocation: '/dev', + browserLocation: '/:path/dev', verify: false, }) diff --git a/examples/next/app/api/[[...routes]]/route.tsx b/examples/next/app/api/[[...routes]]/route.tsx index 14143fba..de4d6d6d 100644 --- a/examples/next/app/api/[[...routes]]/route.tsx +++ b/examples/next/app/api/[[...routes]]/route.tsx @@ -3,7 +3,7 @@ import { Button, Farc, TextInput } from 'farc' import { handle } from 'farc/vercel' -const app = new Farc({ basePath: '/api', browserLocation: '../' }) +const app = new Farc({ basePath: '/api', browserLocation: '../:path' }) app.frame('/', (context) => { const { buttonValue, inputText, status } = context diff --git a/src/farc-base.tsx b/src/farc-base.tsx index 85883729..5c761de0 100644 --- a/src/farc-base.tsx +++ b/src/farc-base.tsx @@ -41,6 +41,46 @@ export type FarcConstructorParameters< * browser by clicking on the link beneath the frame on Warpcast. * We may want to redirect them to a different page (ie. a mint page, etc) * when they arrive via their browser. + * + * @example (Absolute Path) + * Parameters: + * basePath: '/api' + * browserLocation: '/' + * + * https://example.com/api -> https://example.com/ + * https://example.com/api/foo -> https://example.com/ + * + * @example (Absolute Path) + * Parameters: + * basePath: '/api' + * browserLocation: '/mint' + * + * https://example.com/api -> https://example.com/mint + * https://example.com/api/foo -> https://example.com/mint + * + * @example (Absolute Path with `:path` template) + * Parameters + * basePath: '/api' + * browserLocation: '/:path' + * + * https://example.com/api -> https://example.com/ + * https://example.com/api/foo -> https://example.com/foo + * https://example.com/api/foo/bar -> https://example.com/foo/bar + * + * @example (Relative Path) + * Parameters: + * basePath: '/api' + * browserLocation: './dev' + * + * https://example.com/api -> https://example.com/api/dev + * https://example.com/api/foo -> https://example.com/api/foo/dev + * + * @example (URL) + * Parameters: + * browserLocation: 'https://google.com/:path' + * + * https://example.com/api -> https://google.com/api + * https://example.com/api/foo -> https://google.com/api/foo */ browserLocation?: string | undefined /** @@ -232,11 +272,14 @@ export class FarcBase< // If the user is coming from a browser, and a `browserLocation` is set, // then we will redirect the user to that location. const browser = new UAParser(c.req.header('user-agent')).getBrowser() - if (browser.name && browserLocation) + const browserLocation_ = browserLocation + ?.replace(':path', path.replace(/(^\/)|(\/$)/, '')) + .replace('//', '/') + if (browser.name && browserLocation_) return c.redirect( - browserLocation.startsWith('http') - ? browserLocation - : `${url.origin + resolve(this.basePath, browserLocation)}`, + browserLocation_.startsWith('http') + ? browserLocation_ + : `${url.origin + resolve(this.basePath, browserLocation_)}`, 302, )