|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { sendEarlyHints, toLinkValues } from "./early-hints"; |
| 3 | +import type { Asset, FetchEvent } from "./types"; |
| 4 | + |
| 5 | +const assets: Asset[] = [ |
| 6 | + { tag: "link", attrs: { href: "/_build/assets/client.css", rel: "stylesheet" } }, |
| 7 | + { tag: "link", attrs: { href: "/_build/assets/client.js", rel: "modulepreload" } }, |
| 8 | + { tag: "link", attrs: { href: "/_build/assets/漢字-abc.js", rel: "modulepreload" } }, |
| 9 | + { tag: "style", attrs: {}, children: "body {}" }, |
| 10 | + { tag: "link", attrs: { href: "/favicon.ico", rel: "icon" } } |
| 11 | +]; |
| 12 | + |
| 13 | +function createEvent(res: Record<string, unknown>) { |
| 14 | + return { nativeEvent: { node: { res } } } as unknown as FetchEvent; |
| 15 | +} |
| 16 | + |
| 17 | +describe("toLinkValues", () => { |
| 18 | + it("preloads stylesheets and modulepreloads scripts, like the page does", () => { |
| 19 | + expect(toLinkValues(assets)).toEqual([ |
| 20 | + "</_build/assets/client.css>; rel=preload; as=style", |
| 21 | + "</_build/assets/client.js>; rel=modulepreload", |
| 22 | + "</_build/assets/%E6%BC%A2%E5%AD%97-abc.js>; rel=modulepreload" |
| 23 | + ]); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("sendEarlyHints", () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.stubEnv("PROD", true); |
| 30 | + // A build inlines a boolean here. Vitest stores env values as strings, so a |
| 31 | + // test can only set it (truthy) or leave it unset (falsy). |
| 32 | + import.meta.env.START_EARLY_HINTS = true; |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.unstubAllEnvs(); |
| 37 | + delete (import.meta.env as Partial<ImportMetaEnv>).START_EARLY_HINTS; |
| 38 | + }); |
| 39 | + |
| 40 | + it("writes a 103 response on Node", () => { |
| 41 | + const res = { socket: {}, headersSent: false, writeEarlyHints: vi.fn(), appendHeader: vi.fn() }; |
| 42 | + sendEarlyHints(createEvent(res), assets); |
| 43 | + |
| 44 | + expect(res.writeEarlyHints).toHaveBeenCalledWith({ link: toLinkValues(assets) }); |
| 45 | + expect(res.appendHeader).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("adds Link headers instead when the response has no socket", () => { |
| 49 | + const res = { |
| 50 | + socket: null, |
| 51 | + headersSent: false, |
| 52 | + writeEarlyHints: vi.fn(), |
| 53 | + appendHeader: vi.fn() |
| 54 | + }; |
| 55 | + sendEarlyHints(createEvent(res), assets); |
| 56 | + |
| 57 | + expect(res.writeEarlyHints).not.toHaveBeenCalled(); |
| 58 | + expect(res.appendHeader.mock.calls).toEqual(toLinkValues(assets).map(link => ["link", link])); |
| 59 | + }); |
| 60 | + |
| 61 | + it("does nothing when the earlyHints option is off", () => { |
| 62 | + delete (import.meta.env as Partial<ImportMetaEnv>).START_EARLY_HINTS; |
| 63 | + const res = { socket: {}, headersSent: false, writeEarlyHints: vi.fn(), appendHeader: vi.fn() }; |
| 64 | + sendEarlyHints(createEvent(res), assets); |
| 65 | + expect(res.writeEarlyHints).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("does nothing outside production", () => { |
| 69 | + vi.stubEnv("PROD", false); |
| 70 | + const res = { socket: {}, headersSent: false, writeEarlyHints: vi.fn(), appendHeader: vi.fn() }; |
| 71 | + sendEarlyHints(createEvent(res), assets); |
| 72 | + expect(res.writeEarlyHints).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("does nothing once the response headers are sent", () => { |
| 76 | + const res = { socket: {}, headersSent: true, writeEarlyHints: vi.fn(), appendHeader: vi.fn() }; |
| 77 | + sendEarlyHints(createEvent(res), assets); |
| 78 | + expect(res.writeEarlyHints).not.toHaveBeenCalled(); |
| 79 | + expect(res.appendHeader).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments