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