Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions e2e/react-start/csp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.output
dist
*.txt
29 changes: 29 additions & 0 deletions e2e/react-start/csp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "tanstack-react-start-e2e-csp",
"private": true,
"sideEffects": false,
"type": "module",
"scripts": {
"dev": "vite dev --port 3000",
"dev:e2e": "vite dev",
"build": "vite build && tsc --noEmit",
"start": "pnpx srvx --prod -s ../client dist/server/server.js",
"test:e2e": "rm -rf port*.txt; playwright test --project=chromium"
},
"dependencies": {
"@tanstack/react-router": "workspace:^",
"@tanstack/react-start": "workspace:^",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"vite": "^7.1.7"
},
"devDependencies": {
"@playwright/test": "^1.50.1",
"@tanstack/router-e2e-utils": "workspace:^",
"@types/node": "^22.10.2",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"srvx": "^0.10.0",
"typescript": "^5.7.2"
}
}
35 changes: 35 additions & 0 deletions e2e/react-start/csp/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { defineConfig, devices } from '@playwright/test'
import { getTestServerPort } from '@tanstack/router-e2e-utils'
import packageJson from './package.json' with { type: 'json' }

const PORT = await getTestServerPort(packageJson.name)
const baseURL = `http://localhost:${PORT}`

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
workers: 1,

reporter: [['line']],

use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL,
},

webServer: {
command: `VITE_SERVER_PORT=${PORT} pnpm build && PORT=${PORT} VITE_SERVER_PORT=${PORT} pnpm start`,
url: baseURL,
reuseExistingServer: !process.env.CI,
stdout: 'pipe',
},

projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
})
4 changes: 4 additions & 0 deletions e2e/react-start/csp/public/external.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.external-styled {
color: blue;
font-weight: bold;
}
3 changes: 3 additions & 0 deletions e2e/react-start/csp/public/external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This script sets a window global when loaded
// Using a global avoids race conditions with React and DOM ownership issues
window.__EXTERNAL_SCRIPT_LOADED__ = true
68 changes: 68 additions & 0 deletions e2e/react-start/csp/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable */

// @ts-nocheck

// noinspection JSUnusedGlobalSymbols

// This file was automatically generated by TanStack Router.
// You should NOT make any changes in this file as it will be overwritten.
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.

import { Route as rootRouteImport } from './routes/__root'
import { Route as IndexRouteImport } from './routes/index'

const IndexRoute = IndexRouteImport.update({
id: '/',
path: '/',
getParentRoute: () => rootRouteImport,
} as any)

export interface FileRoutesByFullPath {
'/': typeof IndexRoute
}
export interface FileRoutesByTo {
'/': typeof IndexRoute
}
export interface FileRoutesById {
__root__: typeof rootRouteImport
'/': typeof IndexRoute
}
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/'
fileRoutesByTo: FileRoutesByTo
to: '/'
id: '__root__' | '/'
fileRoutesById: FileRoutesById
}
export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
}

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
id: '/'
path: '/'
fullPath: '/'
preLoaderRoute: typeof IndexRouteImport
parentRoute: typeof rootRouteImport
}
}
}

const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
}
export const routeTree = rootRouteImport
._addFileChildren(rootRouteChildren)
._addFileTypes<FileRouteTypes>()

import type { getRouter } from './router.tsx'
import type { createStart } from '@tanstack/react-start'
declare module '@tanstack/react-start' {
interface Register {
ssr: true
router: Awaited<ReturnType<typeof getRouter>>
}
}
20 changes: 20 additions & 0 deletions e2e/react-start/csp/src/router.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createRouter } from '@tanstack/react-router'
import { createIsomorphicFn } from '@tanstack/react-start'
import { routeTree } from './routeTree.gen'

const getSSROptions = createIsomorphicFn().server(() => {
const array = new Uint8Array(16)
crypto.getRandomValues(array)
const nonce = Array.from(array, (b) => b.toString(16).padStart(2, '0')).join(
'',
)
return { nonce }
})

export function getRouter() {
return createRouter({
routeTree,
scrollRestoration: true,
ssr: getSSROptions(),
})
}
47 changes: 47 additions & 0 deletions e2e/react-start/csp/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
createRootRoute,
HeadContent,
Outlet,
Scripts,
} from '@tanstack/react-router'

export const Route = createRootRoute({
headers: ({ ssr }) => {
const nonce = ssr?.nonce
if (!nonce) return
return {
'Content-Security-Policy': [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}'`,
`style-src 'self' 'nonce-${nonce}'`,
].join('; '),
}
},
head: () => ({
meta: [
{ charSet: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ title: 'CSP Nonce Test' },
],
links: [{ rel: 'stylesheet', href: '/external.css' }],
scripts: [{ src: '/external.js' }],
styles: [
{ children: '.inline-styled { color: green; font-weight: bold; }' },
],
}),
component: RootComponent,
})

function RootComponent() {
return (
<html>
<head>
<HeadContent />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
)
}
25 changes: 25 additions & 0 deletions e2e/react-start/csp/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useState } from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
component: Home,
})

function Home() {
const [count, setCount] = useState(0)

return (
<div>
<h1 data-testid="csp-heading">CSP Nonce Test</h1>
<p data-testid="inline-styled" className="inline-styled">
This should be green if inline styles work
</p>
<p data-testid="external-styled" className="external-styled">
This should be blue if external styles work
</p>
<button data-testid="counter-btn" onClick={() => setCount((c) => c + 1)}>
Count: <span data-testid="counter-value">{count}</span>
</button>
</div>
)
}
Loading
Loading