|
1 | 1 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
2 | 2 | import { tmpdir } from 'node:os'; |
3 | 3 | import { join } from 'node:path'; |
| 4 | +import { createRouteRulesMatcher } from 'h3/rules'; |
4 | 5 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
5 | 6 |
|
6 | 7 | import { |
@@ -44,6 +45,13 @@ function callLoad(plugin: any, id: string) { |
44 | 45 | return hook?.handler.call({} as any, id); |
45 | 46 | } |
46 | 47 |
|
| 48 | +function runModule(code: string, imports: Record<string, unknown>) { |
| 49 | + return new Function( |
| 50 | + ...Object.keys(imports), |
| 51 | + code.replace(/^import .+;$/gm, '').replace('export default', 'return'), |
| 52 | + )(...Object.values(imports)); |
| 53 | +} |
| 54 | + |
47 | 55 | describe('analogNitroPlugin', () => { |
48 | 56 | let workspaceRoot: string; |
49 | 57 | let projectRoot: string; |
@@ -245,7 +253,6 @@ describe('analogNitroPlugin', () => { |
245 | 253 | // ship markup pointing at an entry that a build does not emit. |
246 | 254 | expect(code).toContain('/assets/main-abc.js'); |
247 | 255 | expect(code).not.toContain('id=\\"app\\"'); |
248 | | - expect(code).toContain("'x-analog-no-ssr'"); |
249 | 256 | }); |
250 | 257 |
|
251 | 258 | it('fails loudly when a build produced no client document', () => { |
@@ -363,6 +370,263 @@ describe('analogNitroPlugin', () => { |
363 | 370 | }); |
364 | 371 | }); |
365 | 372 |
|
| 373 | + describe('SSR renderer route rules', () => { |
| 374 | + async function createRenderer(routeRules: Record<string, object>) { |
| 375 | + const plugin = analogNitroPlugin({ workspaceRoot }); |
| 376 | + callConfig(plugin, projectRoot, 'serve'); |
| 377 | + const nitroMock: any = { |
| 378 | + options: { |
| 379 | + rootDir: projectRoot, |
| 380 | + buildDir: join(projectRoot, '.nitro'), |
| 381 | + handlers: [], |
| 382 | + scanDirs: [], |
| 383 | + virtual: {}, |
| 384 | + renderer: {}, |
| 385 | + routeRules, |
| 386 | + dev: true, |
| 387 | + }, |
| 388 | + hooks: { hook: vi.fn() }, |
| 389 | + }; |
| 390 | + await (plugin as any).nitro.setup(nitroMock); |
| 391 | + |
| 392 | + const matchRouteRules = createRouteRulesMatcher( |
| 393 | + nitroMock.options.routeRules, |
| 394 | + ); |
| 395 | + const fetch = vi.fn(async (_req: Request) => new Response('rendered')); |
| 396 | + const handler = runModule( |
| 397 | + nitroMock.options.virtual['#analog/ssr-renderer'](), |
| 398 | + { |
| 399 | + defineHandler: (fn: unknown) => fn, |
| 400 | + getRouteRules: matchRouteRules, |
| 401 | + ssr: { fetch }, |
| 402 | + }, |
| 403 | + ); |
| 404 | + const render = async (path: string, headers?: Record<string, string>) => { |
| 405 | + const req = new Request(`http://localhost${path}`, { headers }); |
| 406 | + const waitUntil = vi.fn(); |
| 407 | + const result = await handler({ |
| 408 | + req, |
| 409 | + url: new URL(req.url), |
| 410 | + res: { headers: new Headers() }, |
| 411 | + waitUntil, |
| 412 | + }); |
| 413 | + return { result, forwarded: fetch.mock.calls.at(-1)?.[0], waitUntil }; |
| 414 | + }; |
| 415 | + return { render, fetch }; |
| 416 | + } |
| 417 | + |
| 418 | + it('lets a narrower ssr: true rule override a broader ssr: false', async () => { |
| 419 | + const { render, fetch } = await createRenderer({ |
| 420 | + '/**': { ssr: false }, |
| 421 | + '/live/**': { ssr: true }, |
| 422 | + }); |
| 423 | + |
| 424 | + expect(typeof (await render('/other')).result).toBe('string'); |
| 425 | + expect(fetch).not.toHaveBeenCalled(); |
| 426 | + expect((await render('/live/page')).result).toBeInstanceOf(Response); |
| 427 | + }); |
| 428 | + |
| 429 | + it('lets a narrower streaming: true rule override a broader streaming: false', async () => { |
| 430 | + const { render } = await createRenderer({ |
| 431 | + '/**': { streaming: false }, |
| 432 | + '/live/**': { streaming: true }, |
| 433 | + }); |
| 434 | + |
| 435 | + const buffered = (await render('/other')).forwarded; |
| 436 | + expect(buffered?.headers.get('x-analog-no-streaming')).toBe('true'); |
| 437 | + const streamed = (await render('/live/page')).forwarded; |
| 438 | + expect(streamed?.headers.has('x-analog-no-streaming')).toBe(false); |
| 439 | + }); |
| 440 | + |
| 441 | + it('ignores client-sent ssr and streaming opt-out headers', async () => { |
| 442 | + const { render } = await createRenderer({}); |
| 443 | + |
| 444 | + const { result, forwarded } = await render('/page', { |
| 445 | + 'x-analog-no-ssr': 'true', |
| 446 | + 'x-analog-no-streaming': 'true', |
| 447 | + accept: 'text/html', |
| 448 | + }); |
| 449 | + |
| 450 | + expect(result).toBeInstanceOf(Response); |
| 451 | + expect(forwarded?.headers.has('x-analog-no-ssr')).toBe(false); |
| 452 | + expect(forwarded?.headers.has('x-analog-no-streaming')).toBe(false); |
| 453 | + expect(forwarded?.headers.get('accept')).toBe('text/html'); |
| 454 | + }); |
| 455 | + |
| 456 | + it('forwards waitUntil to the SSR service', async () => { |
| 457 | + const { render } = await createRenderer({}); |
| 458 | + |
| 459 | + const { forwarded, waitUntil } = await render('/page'); |
| 460 | + const task = Promise.resolve(); |
| 461 | + ( |
| 462 | + forwarded as Request & { waitUntil(task: Promise<void>): void } |
| 463 | + ).waitUntil(task); |
| 464 | + |
| 465 | + expect(waitUntil).toHaveBeenCalledWith(task); |
| 466 | + }); |
| 467 | + }); |
| 468 | + |
| 469 | + describe('SSR entry', () => { |
| 470 | + afterEach(() => { |
| 471 | + vi.unstubAllGlobals(); |
| 472 | + vi.restoreAllMocks(); |
| 473 | + }); |
| 474 | + |
| 475 | + function createEntry(renderer: (...args: any[]) => unknown) { |
| 476 | + vi.stubGlobal('$fetch', vi.fn()); |
| 477 | + const plugin = analogNitroPlugin({ workspaceRoot }); |
| 478 | + callConfig(plugin, projectRoot, 'serve'); |
| 479 | + return runModule( |
| 480 | + callLoad(plugin, '\0virtual:@analogjs/nitro/ssr-entry'), |
| 481 | + { |
| 482 | + nitroServerFetch: vi.fn(), |
| 483 | + createFetch: () => vi.fn(), |
| 484 | + renderer, |
| 485 | + }, |
| 486 | + ); |
| 487 | + } |
| 488 | + |
| 489 | + it('responds with the status of a failed render', async () => { |
| 490 | + const entry = createEntry(() => { |
| 491 | + throw Object.assign(new Error('Not found'), { statusCode: 404 }); |
| 492 | + }); |
| 493 | + vi.spyOn(console, 'error').mockImplementation(() => undefined); |
| 494 | + |
| 495 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 496 | + |
| 497 | + expect(response.status).toBe(404); |
| 498 | + expect(response.headers.get('cache-control')).toBe('no-store'); |
| 499 | + }); |
| 500 | + |
| 501 | + it('responds with the status the app set on the response', async () => { |
| 502 | + const entry = createEntry((_url, _template, { res }) => { |
| 503 | + res.statusCode = 404; |
| 504 | + return '<html></html>'; |
| 505 | + }); |
| 506 | + |
| 507 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 508 | + |
| 509 | + expect(response.status).toBe(404); |
| 510 | + }); |
| 511 | + |
| 512 | + it('passes the request signal and waitUntil to the renderer', async () => { |
| 513 | + let context: any; |
| 514 | + const entry = createEntry((_url, _template, ctx) => { |
| 515 | + context = ctx; |
| 516 | + return '<html></html>'; |
| 517 | + }); |
| 518 | + const abort = new AbortController(); |
| 519 | + const waitUntil = vi.fn(); |
| 520 | + const request = Object.assign( |
| 521 | + new Request('http://localhost/x', { signal: abort.signal }), |
| 522 | + { waitUntil }, |
| 523 | + ); |
| 524 | + |
| 525 | + await entry.fetch(request); |
| 526 | + abort.abort(); |
| 527 | + const task = Promise.resolve(); |
| 528 | + context.waitUntil(task); |
| 529 | + |
| 530 | + expect(context.signal.aborted).toBe(true); |
| 531 | + expect(waitUntil).toHaveBeenCalledWith(task); |
| 532 | + expect(context.renderErrorsAsHtml).toBe(true); |
| 533 | + }); |
| 534 | + |
| 535 | + it('responds with the headers the app set on the response', async () => { |
| 536 | + const entry = createEntry((_url, _template, { res }) => { |
| 537 | + res.setHeader('cache-control', 'max-age=60'); |
| 538 | + res.setHeader('set-cookie', ['a=1', 'b=2']); |
| 539 | + res.setHeader('x-removed', 'yes'); |
| 540 | + res.removeHeader('x-removed'); |
| 541 | + return '<html></html>'; |
| 542 | + }); |
| 543 | + |
| 544 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 545 | + |
| 546 | + expect(response.headers.get('cache-control')).toBe('max-age=60'); |
| 547 | + expect(response.headers.getSetCookie()).toEqual(['a=1', 'b=2']); |
| 548 | + expect(response.headers.has('x-removed')).toBe(false); |
| 549 | + expect(response.headers.get('content-type')).toBe( |
| 550 | + 'text/html; charset=utf-8', |
| 551 | + ); |
| 552 | + }); |
| 553 | + |
| 554 | + it('reads set-cookie back as a list like Node does', async () => { |
| 555 | + let cookies: unknown; |
| 556 | + const entry = createEntry((_url, _template, { res }) => { |
| 557 | + res.setHeader('Set-Cookie', ['a=1; Path=/', 'b=2']); |
| 558 | + cookies = res.getHeader('set-cookie'); |
| 559 | + return '<html></html>'; |
| 560 | + }); |
| 561 | + |
| 562 | + await entry.fetch(new Request('http://localhost/x')); |
| 563 | + |
| 564 | + expect(cookies).toEqual(['a=1; Path=/', 'b=2']); |
| 565 | + }); |
| 566 | + |
| 567 | + it('reads an unset set-cookie as undefined like Node does', async () => { |
| 568 | + let cookies: unknown = 'unread'; |
| 569 | + const entry = createEntry((_url, _template, { res }) => { |
| 570 | + cookies = res.getHeader('set-cookie'); |
| 571 | + return '<html></html>'; |
| 572 | + }); |
| 573 | + |
| 574 | + await entry.fetch(new Request('http://localhost/x')); |
| 575 | + |
| 576 | + expect(cookies).toBeUndefined(); |
| 577 | + }); |
| 578 | + |
| 579 | + it('responds without a body for null-body statuses', async () => { |
| 580 | + const entry = createEntry((_url, _template, { res }) => { |
| 581 | + res.statusCode = 304; |
| 582 | + return '<html></html>'; |
| 583 | + }); |
| 584 | + |
| 585 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 586 | + |
| 587 | + expect(response.status).toBe(304); |
| 588 | + expect(response.body).toBeNull(); |
| 589 | + }); |
| 590 | + |
| 591 | + it('cancels a stream it cannot send for a null-body status', async () => { |
| 592 | + const cancel = vi.fn(); |
| 593 | + const entry = createEntry((_url, _template, { res }) => { |
| 594 | + res.statusCode = 304; |
| 595 | + return new ReadableStream({ cancel }); |
| 596 | + }); |
| 597 | + |
| 598 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 599 | + |
| 600 | + expect(response.status).toBe(304); |
| 601 | + expect(cancel).toHaveBeenCalled(); |
| 602 | + expect(response.headers.has('cache-control')).toBe(false); |
| 603 | + }); |
| 604 | + |
| 605 | + it('leaves buffered renders cacheable', async () => { |
| 606 | + const entry = createEntry(() => |
| 607 | + Object.assign(new ReadableStream(), { |
| 608 | + [Symbol.for('analog.bufferedStream')]: true, |
| 609 | + }), |
| 610 | + ); |
| 611 | + |
| 612 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 613 | + |
| 614 | + expect(response.headers.has('cache-control')).toBe(false); |
| 615 | + expect(response.headers.has('content-encoding')).toBe(false); |
| 616 | + }); |
| 617 | + |
| 618 | + it('keeps streamed responses from being buffered or cached', async () => { |
| 619 | + const entry = createEntry(() => new ReadableStream()); |
| 620 | + |
| 621 | + const response = await entry.fetch(new Request('http://localhost/x')); |
| 622 | + |
| 623 | + expect(response.headers.get('content-encoding')).toBe('identity'); |
| 624 | + expect(response.headers.get('cache-control')).toBe( |
| 625 | + 'no-store, no-transform', |
| 626 | + ); |
| 627 | + }); |
| 628 | + }); |
| 629 | + |
366 | 630 | it('stamps route rule headers for ssr and streaming opt-outs', () => { |
367 | 631 | const nitroMock: any = { |
368 | 632 | options: { |
|
0 commit comments