Skip to content

Commit

Permalink
feat: support browserLocation path template
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 18, 2024
1 parent 042a347 commit 22016e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"files": {
"ignore": [
"coverage",
".next",
"node_modules",
"src/_lib",
"src/vendor",
"tsconfig.json",
"tsconfig.*.json"
Expand Down
2 changes: 1 addition & 1 deletion examples/_dev/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
2 changes: 1 addition & 1 deletion examples/next/app/api/[[...routes]]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 47 additions & 4 deletions src/farc-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down Expand Up @@ -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,
)

Expand Down

0 comments on commit 22016e1

Please sign in to comment.