Platformatic React Router
The Platformatic React Router allows to run a React Router application as a Platformatic Runtime application with no modifications.
Getting Started
Create or copy a React Router Start application inside the applications, services or web folder. If you are not using autoload, you also have to explictly add the new application.
You are all set, you can now start your runtime as usual via wattpm dev or wattpm start.
Install
npm install @platformatic/react-router
Example configuration file
Create a watt.json in the root folder of your application with the following contents:
{
"$schema": "https://schemas.platformatic.dev/@platformatic/react-router/3.30.0.json",
"application": {
"basePath": "/frontend"
}
}
Using when the entrypoint is a Platformatic Gateway
To properly work when using with in application where the entrypoint is a Platformatic Gateway, you need to adjust your react-router.config.ts file to use the Platformatic base path:
import type { Config } from '@react-router/dev/config'
import { getBasePath } from '@platformatic/globals'
export default {
basename: getBasePath({ throwOnMissing: false }) ?? '/'
ssr: true
} satisfies Config
You also need to adjust the base option in your vite.config.ts:
import { getBasePath } from '@platformatic/globals'
import { reactRouter } from '@react-router/dev/vite'
import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'
export default defineConfig({
base: getBasePath({ throwOnMissing: false }) ?? '/',
plugins: [reactRouter(), tsconfigPaths()]
})
Providing a custom entrypoint
If you want provide a custom entrypoint which will be used in @react-router/node, you need to make three configuration changes:
-
Modify your Vite configuration to properly handle SSR builds by making it dependent on the SSR flags:
import { getBasePath } from '@platformatic/globals'
import { reactRouter } from '@react-router/dev/vite'
import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'
export default defineConfig(({ isSsrBuild }) => ({
base: getBasePath({ throwOnMissing: false }) ?? '/',
build: {
rollupOptions: isSsrBuild ? { input: './app/server.ts' } : undefined
},
plugins: [reactRouter(), tsconfigPaths()],
server: {
fs: {
strict: false
},
allowedHosts: ['.plt.local']
}
})) -
Create an
app/server.tsfile that exports anentrypointvariable:export const entrypoint = import('virtual:react-router/server-build')This file serves as the SSR entrypoint for the server build and is referenced in the Vite configuration.