|
1 | 1 | import { parse } from "url";
|
2 | 2 | import createNextServer from "next";
|
3 |
| -import LRU from "lru-cache"; |
4 | 3 |
|
5 | 4 | import type { Request } from "firebase-functions/v2/https";
|
6 | 5 | import type { Response } from "express";
|
7 | 6 | import type { NextServer } from "next/dist/server/next.js";
|
| 7 | +import { incomingMessageFromExpress } from "../utils.js"; |
8 | 8 |
|
9 |
| -const nextAppsLRU = new LRU<string, NextServer>({ |
10 |
| - // TODO tune this |
11 |
| - max: 3, |
12 |
| - allowStale: true, |
13 |
| - updateAgeOnGet: true, |
14 |
| - dispose: (server) => { |
15 |
| - server.close(); |
16 |
| - }, |
| 9 | +// @ts-expect-error - Next.js doesn't export the custom server function with proper types |
| 10 | +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 11 | +const nextApp: NextServer = createNextServer({ |
| 12 | + dev: false, |
| 13 | + dir: process.cwd(), |
| 14 | + hostname: "0.0.0.0", |
| 15 | + port: 443, |
17 | 16 | });
|
18 | 17 |
|
19 | 18 | export const handle = async (req: Request, res: Response) => {
|
20 |
| - const { hostname, protocol, url } = req; |
21 |
| - const port = protocol === "https" ? 443 : 80; |
22 |
| - const key = [hostname, port].join(":"); |
23 |
| - // I wish there was a better way to do this, but it seems like this is the |
24 |
| - // way to go. Should investigate more if we can get hostname/port to be |
25 |
| - // dynamic for middleware. |
26 |
| - let nextApp = nextAppsLRU.get(key); |
27 |
| - if (!nextApp) { |
28 |
| - // @ts-expect-error - Next.js doesn't export the custom server function with proper types |
29 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
30 |
| - nextApp = createNextServer({ |
31 |
| - dev: false, |
32 |
| - dir: process.cwd(), |
33 |
| - hostname: "0.0.0.0", |
34 |
| - port, |
35 |
| - }); |
36 |
| - nextAppsLRU.set(key, nextApp!); |
37 |
| - } |
38 |
| - await nextApp!.prepare(); |
39 |
| - const parsedUrl = parse(url, true); |
40 |
| - nextApp!.getRequestHandler()(req, res, parsedUrl); |
| 19 | + await nextApp.prepare(); |
| 20 | + const parsedUrl = parse(req.url, true); |
| 21 | + const incomingMessage = incomingMessageFromExpress(req); |
| 22 | + await nextApp.getRequestHandler()(incomingMessage, res, parsedUrl); |
41 | 23 | };
|
0 commit comments