Skip to content

Commit 4b15b47

Browse files
fix Paraglide Start scaffold integration
1 parent 3f38875 commit 4b15b47

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

.changeset/fuzzy-ravens-route.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/create': patch
3+
---
4+
5+
Configure the Paraglide add-on with TanStack Router URL rewrites and request-scoped server middleware.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { paraglideMiddleware } from './paraglide/server.js'
2+
import handler from '@tanstack/react-start/server-entry'
3+
4+
export default {
5+
fetch(req: Request): Promise<Response> {
6+
return paraglideMiddleware(req, () => handler.fetch(req))
7+
},
8+
}

packages/create/src/frameworks/react/project/base/src/router.tsx.ejs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
22
import { routeTree } from './routeTree.gen'
3+
<% if (addOnEnabled.paraglide) { %>
4+
import { deLocalizeUrl, localizeUrl } from './paraglide/runtime'
5+
<% } %>
36
<% if (addOnEnabled['tanstack-query']) { %>
47
import type { ReactNode } from 'react'
58
import { QueryClient } from '@tanstack/react-query'
@@ -20,6 +23,12 @@ export function getRouter() {
2023
scrollRestoration: true,
2124
defaultPreload: 'intent',
2225
defaultPreloadStaleTime: 0,
26+
<% if (addOnEnabled.paraglide) { %>
27+
rewrite: {
28+
input: ({ url }) => deLocalizeUrl(url),
29+
output: ({ url }) => localizeUrl(url),
30+
},
31+
<% } %>
2332
<% if (addOnEnabled.tRPC) { %>
2433
Wrap: (props: { children: ReactNode }) => {
2534
return (
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { afterEach, describe, expect, it, vi } from 'vitest'
2+
3+
import {
4+
finalizeAddOns,
5+
populateAddOnOptionsDefaults,
6+
} from '../src/add-ons.js'
7+
import { createApp } from '../src/create-app.js'
8+
import { createMemoryEnvironment } from '../src/environment.js'
9+
import { createFrameworkDefinition } from '../src/frameworks/react/index.js'
10+
11+
import type { Framework, FrameworkDefinition, Options } from '../src/types.js'
12+
13+
function frameworkFromDefinition(definition: FrameworkDefinition): Framework {
14+
const { addOns, base, ...framework } = definition
15+
16+
return {
17+
...framework,
18+
getFiles: () => Promise.resolve(Object.keys(base)),
19+
getFileContents: (path: string) => Promise.resolve(base[path]),
20+
getDeletedFiles: () => Promise.resolve([]),
21+
getAddOns: () => addOns,
22+
}
23+
}
24+
25+
afterEach(() => {
26+
vi.unstubAllGlobals()
27+
})
28+
29+
describe('Paraglide add-on', () => {
30+
it('configures localized routing and request-scoped SSR', async () => {
31+
vi.stubGlobal(
32+
'fetch',
33+
vi.fn(
34+
async () =>
35+
new Response(JSON.stringify({ version: '1.0.0' }), { status: 200 }),
36+
),
37+
)
38+
39+
const framework = frameworkFromDefinition(createFrameworkDefinition())
40+
const chosenAddOns = await finalizeAddOns(framework, 'file-router', [
41+
'paraglide',
42+
])
43+
const options = {
44+
projectName: 'paraglide-app',
45+
targetDir: '/paraglide-app',
46+
framework,
47+
mode: 'file-router',
48+
typescript: true,
49+
tailwind: true,
50+
packageManager: 'pnpm',
51+
git: false,
52+
install: false,
53+
intent: false,
54+
includeExamples: true,
55+
chosenAddOns,
56+
addOnOptions: populateAddOnOptionsDefaults(chosenAddOns),
57+
} as Options
58+
const { environment, output } = createMemoryEnvironment()
59+
60+
await createApp(environment, options)
61+
62+
const router = output.files['/paraglide-app/src/router.tsx']
63+
expect(router).toContain(
64+
"import { deLocalizeUrl, localizeUrl } from './paraglide/runtime'",
65+
)
66+
expect(router).toContain('input: ({ url }) => deLocalizeUrl(url)')
67+
expect(router).toContain('output: ({ url }) => localizeUrl(url)')
68+
69+
const server = output.files['/paraglide-app/src/server.ts']
70+
expect(server).toContain(
71+
"import { paraglideMiddleware } from './paraglide/server.js'",
72+
)
73+
expect(server).toContain(
74+
"import handler from '@tanstack/react-start/server-entry'",
75+
)
76+
expect(server).toContain(
77+
'return paraglideMiddleware(req, () => handler.fetch(req))',
78+
)
79+
})
80+
})

0 commit comments

Comments
 (0)