tldraw/apps/dotcom/src/main.tsx

32 lines
1 KiB
TypeScript
Raw Normal View History

import { Analytics as VercelAnalytics } from '@vercel/analytics/react'
import { createRoot } from 'react-dom/client'
import { HelmetProvider } from 'react-helmet-async'
Make Vercel URL rewrites precise (#2913) ### The problem Right now we use a catchall path in Vercel routing config to rewrite all requests that don't match existing assets to `/index.html`, which is needed for client side routing to work. This, however, messes up 404 errors for truly non-existing files which won't be handled by the SPA, because they get redirected to index.html. Even worse, this interacts very poorly with caching. Normally if we request a non-existent file, then put the file in place, and request the file again, we'll get 404 the first time and the actual file the second time. However, in our case we instead return `/index.html` after the first attempt and cache that response, making it impossible to correct a missing file without cache flush. ### The solution One way to fix this is to make the regex in Vercel config precise, so that they only match our SPA routes. However, it can be dangerous, because this means we'll need to manually update the config with new SPA routes every time we add any. This PR tests that regexes we're using in Vercel match all routes that we set in the SPA router. ### Potential future improvements It's very possible to generate Vercel's config from React Router routing objects, but at the moment it's not done because that would require importing most of dotcom during the build phase, which seem to cause errors. ### Change Type - [x] `internal` — Any other changes that don't affect the published package[^2] ### Test Plan 1. Might need a light smoke test after deployment to dotcom. - [x] End to end tests
2024-02-22 18:25:45 +00:00
import { RouterProvider, createBrowserRouter } from 'react-router-dom'
import '../sentry.client.config'
import '../styles/core.css'
Make Vercel URL rewrites precise (#2913) ### The problem Right now we use a catchall path in Vercel routing config to rewrite all requests that don't match existing assets to `/index.html`, which is needed for client side routing to work. This, however, messes up 404 errors for truly non-existing files which won't be handled by the SPA, because they get redirected to index.html. Even worse, this interacts very poorly with caching. Normally if we request a non-existent file, then put the file in place, and request the file again, we'll get 404 the first time and the actual file the second time. However, in our case we instead return `/index.html` after the first attempt and cache that response, making it impossible to correct a missing file without cache flush. ### The solution One way to fix this is to make the regex in Vercel config precise, so that they only match our SPA routes. However, it can be dangerous, because this means we'll need to manually update the config with new SPA routes every time we add any. This PR tests that regexes we're using in Vercel match all routes that we set in the SPA router. ### Potential future improvements It's very possible to generate Vercel's config from React Router routing objects, but at the moment it's not done because that would require importing most of dotcom during the build phase, which seem to cause errors. ### Change Type - [x] `internal` — Any other changes that don't affect the published package[^2] ### Test Plan 1. Might need a light smoke test after deployment to dotcom. - [x] End to end tests
2024-02-22 18:25:45 +00:00
import '../styles/globals.css'
import { Head } from './components/Head/Head'
Make Vercel URL rewrites precise (#2913) ### The problem Right now we use a catchall path in Vercel routing config to rewrite all requests that don't match existing assets to `/index.html`, which is needed for client side routing to work. This, however, messes up 404 errors for truly non-existing files which won't be handled by the SPA, because they get redirected to index.html. Even worse, this interacts very poorly with caching. Normally if we request a non-existent file, then put the file in place, and request the file again, we'll get 404 the first time and the actual file the second time. However, in our case we instead return `/index.html` after the first attempt and cache that response, making it impossible to correct a missing file without cache flush. ### The solution One way to fix this is to make the regex in Vercel config precise, so that they only match our SPA routes. However, it can be dangerous, because this means we'll need to manually update the config with new SPA routes every time we add any. This PR tests that regexes we're using in Vercel match all routes that we set in the SPA router. ### Potential future improvements It's very possible to generate Vercel's config from React Router routing objects, but at the moment it's not done because that would require importing most of dotcom during the build phase, which seem to cause errors. ### Change Type - [x] `internal` — Any other changes that don't affect the published package[^2] ### Test Plan 1. Might need a light smoke test after deployment to dotcom. - [x] End to end tests
2024-02-22 18:25:45 +00:00
import { router } from './routes'
Make Vercel URL rewrites precise (#2913) ### The problem Right now we use a catchall path in Vercel routing config to rewrite all requests that don't match existing assets to `/index.html`, which is needed for client side routing to work. This, however, messes up 404 errors for truly non-existing files which won't be handled by the SPA, because they get redirected to index.html. Even worse, this interacts very poorly with caching. Normally if we request a non-existent file, then put the file in place, and request the file again, we'll get 404 the first time and the actual file the second time. However, in our case we instead return `/index.html` after the first attempt and cache that response, making it impossible to correct a missing file without cache flush. ### The solution One way to fix this is to make the regex in Vercel config precise, so that they only match our SPA routes. However, it can be dangerous, because this means we'll need to manually update the config with new SPA routes every time we add any. This PR tests that regexes we're using in Vercel match all routes that we set in the SPA router. ### Potential future improvements It's very possible to generate Vercel's config from React Router routing objects, but at the moment it's not done because that would require importing most of dotcom during the build phase, which seem to cause errors. ### Change Type - [x] `internal` — Any other changes that don't affect the published package[^2] ### Test Plan 1. Might need a light smoke test after deployment to dotcom. - [x] End to end tests
2024-02-22 18:25:45 +00:00
const browserRouter = createBrowserRouter(router)
createRoot(document.getElementById('root')!).render(
<HelmetProvider>
<Head />
Make Vercel URL rewrites precise (#2913) ### The problem Right now we use a catchall path in Vercel routing config to rewrite all requests that don't match existing assets to `/index.html`, which is needed for client side routing to work. This, however, messes up 404 errors for truly non-existing files which won't be handled by the SPA, because they get redirected to index.html. Even worse, this interacts very poorly with caching. Normally if we request a non-existent file, then put the file in place, and request the file again, we'll get 404 the first time and the actual file the second time. However, in our case we instead return `/index.html` after the first attempt and cache that response, making it impossible to correct a missing file without cache flush. ### The solution One way to fix this is to make the regex in Vercel config precise, so that they only match our SPA routes. However, it can be dangerous, because this means we'll need to manually update the config with new SPA routes every time we add any. This PR tests that regexes we're using in Vercel match all routes that we set in the SPA router. ### Potential future improvements It's very possible to generate Vercel's config from React Router routing objects, but at the moment it's not done because that would require importing most of dotcom during the build phase, which seem to cause errors. ### Change Type - [x] `internal` — Any other changes that don't affect the published package[^2] ### Test Plan 1. Might need a light smoke test after deployment to dotcom. - [x] End to end tests
2024-02-22 18:25:45 +00:00
<RouterProvider router={browserRouter} />
<VercelAnalytics debug={false} />
</HelmetProvider>
)
try {
// we have a dummy service worker that unregisters itself immediately
// this was needed to remove the service worker we used to have from the cache
// we can remove this if we ever need a service worker again, or if enough time passes that
// anybody returning to tldraw.com should not have a service worker running
navigator.serviceWorker.register('/sw.js', {
scope: '/',
})
} catch (e) {
// ignore
}