wwwwwwwwwwwwwwwwwww

Utility Functions

All functions listed below are exported directly by one.

redirect

A function that takes two arguments: a string path, and a number status.

import { redirect } from 'one'
export function redirectToLogin() {
return redirect('/login')
}

It accepts relative paths by using the getURL helper internally.

On the server (including in loaders) it returns a Response.redirect object. On the client it calls router.navigate and returns void.

getURL

A function that takes no arguments and returns a string of the current URL of the running application on client or server.

For example, in dev-mode by default this would be a string of http://127.0.0.1:8081. In non-development environments you will need to set process.env.ONE_SERVER_URL to your production URL with no trailing /.

isResponse

One uses Request/Response type objects for API routes, but for some environments doing an instanceof Response can fail, isResponse takes any value and returns true if it is a Response-like object. Useful for API utility functions.

href

A simple function allows for creating typed route strings. It's a type-level only check, at runtime it only validates that it is a string.

import { href } from 'one'
const postPageLink = href(`/post/hello-world`) // will type error if invalid Href

setServerData, getServerData

Developing

For improving performance of client hydration on the web, you can pass data from the server to the client with requestCache, on native values will always be undefined. Data must be JSON-stringifyable and on native.

Here's an example of a simple useFetch to show how it's useful:

app/index.tsx

import { setServerData, getServerData } from 'one'
type SafeURLs = `${`https://tamagui.dev` | `http://localhost`}${string}
const useFetch = async (url: SafeURLs) => {
if (process.env.VITE_ENVIRONMENT === 'ssr') {
// on server data must be set during render
setServerData(url, await fetch(url).then(res => res.json()))
}
return getServerData(url)
}
export default async (props) => {
const serverData = await useFetch(props.url)
return <div />
}
// can use it in loaders, too
export const loader = async ({ params }) => {
await doSomething()
setServerData(params.idl, 'data')
return {}
}

Edit this page on GitHub.