Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSC: Add middleware to kitchen-sink #10735

Merged
merged 4 commits into from
Jun 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export const ServerEntry: React.FC<Props> = ({ css, meta, location }) => {
</Document>
)
}

export async function registerMiddleware() {
const { middleware: selfMtsMw } = await import('./middleware/self.mjs')

return [selfMtsMw]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'

import type {
MiddlewareRequest,
MiddlewareResponse,
} from '@redwoodjs/vite/middleware'

const __filename = fileURLToPath(import.meta.url)

export async function middleware(
req: MiddlewareRequest,
mwResponse: MiddlewareResponse
) {
console.log('self.mts Middleware')
console.log('self.mts req.url:', req.url)

const url = new URL(req.url)
if (url.pathname !== '/self.mts') {
return mwResponse
}

const selfTs = fs.readFileSync(__filename, 'utf8')

mwResponse.body = selfTs

return mwResponse
}
10 changes: 10 additions & 0 deletions tasks/smoke-tests/rsc-kitchen-sink/tests/rsc-kitchen-sink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,13 @@ test('Server Cell in Layout', async ({ page }) => {
// above the code block and then once more inside the codeblock itself
expect(mainText.match(/The source of this server cell/g)).toHaveLength(2)
})

test('middleware', async ({ page }) => {
await page.goto('/self.mts')

const bodyText = await page.locator('body').innerText()

expect(bodyText).toMatch(/import { fileURLToPath } from "node:url"/)
expect(bodyText).toMatch(/self\.mts Middleware/)
expect(bodyText).toMatch(/\.readFileSync\(__filename/)
})
Loading