Skip to content

Commit de028c6

Browse files
committed
Merge branch 'main' into tn/blob
2 parents c38e6bd + 6c28ebc commit de028c6

File tree

17 files changed

+8353
-7450
lines changed

17 files changed

+8353
-7450
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ indent_style = space
55
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
8-
max_line_length = 120
8+
max_line_length = 100
99
trim_trailing_whitespace = true
1010
insert_final_newline = true

.eslintrc.cjs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ module.exports = {
66
sourceType: 'module',
77
},
88
rules: {
9+
'arrow-body-style': 'off',
910
'no-param-reassign': ['error', { props: false }],
1011
'no-underscore-dangle': 'off',
11-
'n/no-sync': 'off',
12-
'n/prefer-global/process': 'off',
1312
'no-magic-numbers': 'off',
13+
'n/prefer-global/process': 'off',
1414
'unicorn/numeric-separators-style': 'off',
1515
'unicorn/filename-case': ['error', { case: 'kebabCase' }],
1616
'import/no-namespace': 'off',
@@ -19,17 +19,9 @@ module.exports = {
1919
overrides: [
2020
...overrides,
2121
{
22-
files: ['src/templates/**'],
22+
files: ['src/handlers/**'],
2323
rules: {
24-
'@typescript-eslint/no-var-requires': 'off',
25-
'@typescript-eslint/ban-ts-comment': 'off',
26-
'unicorn/no-abusive-eslint-disable': 'off',
27-
'eslint-comments/no-unlimited-disable': 'off',
28-
'import/no-unresolved': 'off',
29-
'import/no-unassigned-import': 'off',
3024
'import/no-anonymous-default-export': 'off',
31-
'n/no-missing-require': 'off',
32-
'n/no-missing-import': 'off',
3325
},
3426
},
3527
],

.prettierrc.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
...require('@netlify/eslint-config-node/.prettierrc.json'),
3+
printWidth: 100,
4+
}

.prettierrc.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

Lines changed: 8157 additions & 7161 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@
3434
},
3535
"homepage": "https://github.com/netlify/next-runtime-minimal#readme",
3636
"dependencies": {
37-
"@fastly/http-compute-js": "^1.1.0",
37+
"@fastly/http-compute-js": "github:orinokai/http-compute-js",
3838
"@netlify/blobs": "^1.6.0",
3939
"@netlify/build": "^29.20.6",
4040
"@netlify/functions": "^2.0.1",
41-
"chalk": "^5.3.0",
41+
"@vercel/nft": "^0.24.3",
4242
"fs-extra": "^11.1.1",
43-
"outdent": "^0.8.0",
44-
"esbuild": "^0.19.4"
43+
"globby": "^13.2.2"
4544
},
4645
"devDependencies": {
4746
"@netlify/eslint-config-node": "^7.0.1",

src/handlers/cache.cts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { CacheHandler, CacheHandlerContext } from 'next/dist/server/lib/incremental-cache/index.js'
2+
3+
/**
4+
* Netlify Cache Handler
5+
* (CJS format because Next.js doesn't support ESM yet)
6+
*/
7+
export default class NetlifyCacheHandler implements CacheHandler {
8+
options: CacheHandlerContext
9+
10+
constructor(options: CacheHandlerContext) {
11+
this.options = options
12+
}
13+
14+
// eslint-disable-next-line require-await, class-methods-use-this
15+
public async get(key: string, ctx: any) {
16+
console.log('NetlifyCacheHandler.get', key, JSON.stringify(ctx, null, 2))
17+
return null
18+
}
19+
20+
// eslint-disable-next-line require-await, class-methods-use-this
21+
public async set(key: string, data: any, ctx: any) {
22+
console.log('NetlifyCacheHandler.set', key, JSON.stringify(data, null, 2), JSON.stringify(ctx, null, 2))
23+
}
24+
25+
// eslint-disable-next-line class-methods-use-this, require-await
26+
public async revalidateTag(tag: string) {
27+
console.log('NetlifyCacheHandler.revalidateTag', tag)
28+
}
29+
}

src/handlers/server.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { toComputeResponse, toReqRes } from '@fastly/http-compute-js'
2+
import type { WorkerRequestHandler } from 'next/dist/server/lib/types.js'
3+
4+
import { TASK_DIR } from '../helpers/constants.js'
5+
6+
let nextHandler: WorkerRequestHandler
7+
8+
export default async (request: Request) => {
9+
if (!nextHandler) {
10+
// set the server config
11+
const { setRequestConfig } = await import('../helpers/config.js')
12+
await setRequestConfig()
13+
14+
// let Next.js initialize and create the request handler
15+
const { getRequestHandlers } = await import('next/dist/server/lib/start-server.js')
16+
;[nextHandler] = await getRequestHandlers({
17+
port: 3000,
18+
hostname: 'localhost',
19+
dir: TASK_DIR,
20+
isDev: false,
21+
})
22+
}
23+
24+
const { req, res } = toReqRes(request)
25+
26+
try {
27+
console.log('Next server request:', req.url)
28+
await nextHandler(req, res)
29+
} catch (error) {
30+
console.error(error)
31+
res.statusCode = 500
32+
res.end('Internal Server Error')
33+
}
34+
35+
// log the response from Next.js
36+
const response = { headers: res.getHeaders(), statusCode: res.statusCode }
37+
console.log('Next server response:', JSON.stringify(response, null, 2))
38+
39+
return toComputeResponse(res)
40+
}

src/helpers/blob-store.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/helpers/config.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
import { readFileSync } from 'fs'
1+
import { readFile } from 'node:fs/promises'
22

3-
import type { NetlifyConfig } from '@netlify/build'
4-
import { copySync, moveSync } from 'fs-extra/esm'
5-
6-
import { MODULE_DIR, NETLIFY_PUBLISH_DIR, NETLIFY_TEMP_DIR } from './constants.js'
3+
import { TASK_DIR } from './constants.js'
74

85
/**
9-
* Modify the user's next.config.js to use standalone mode and cache handler
6+
* Enable standalone mode at build-time
107
*/
11-
export const modifyNextConfig = () => {
12-
// revert any previous changes
13-
revertNextConfig()
14-
15-
// TODO: find a better way to do this because there's a ton of different ways
16-
// to configure next.config.js and the user could be using any of them
17-
// https://github.com/netlify/next-runtime-minimal/issues/12
18-
moveSync('next.config.js', `${NETLIFY_TEMP_DIR}/next.config.js`)
19-
copySync(`${MODULE_DIR}/../templates/next-config.cjs`, 'next.config.js')
20-
}
21-
22-
export const revertNextConfig = () => {
23-
// check if modified, then revert
24-
if (readFileSync('next.config.js').includes('Netlify generated code')) {
25-
moveSync(`${NETLIFY_TEMP_DIR}/next.config.js`, 'next.config.js', { overwrite: true })
26-
}
8+
export const setBuildConfig = () => {
9+
process.env.NEXT_PRIVATE_STANDALONE = 'true'
2710
}
2811

2912
/**
30-
* Modify the user's netlify.toml to use our new publish directory
31-
* @param config Netlify config
13+
* Configure the request-time custom cache handler
3214
*/
33-
export const modifyNetlifyConfig = (config: NetlifyConfig) => {
34-
// TODO: once onEnd is fixed, we can remove this
35-
// https://github.com/netlify/cli/issues/6050
36-
config.build.publish = NETLIFY_PUBLISH_DIR
15+
export const setRequestConfig = async () => {
16+
// get config from the build output
17+
const runtimeConfig = JSON.parse(await readFile(`${TASK_DIR}/.next/required-server-files.json`, 'utf-8'))
18+
19+
// set the path to the cache handler
20+
runtimeConfig.config.experimental = {
21+
...runtimeConfig.config.experimental,
22+
incrementalCacheHandlerPath: `${TASK_DIR}/dist/handlers/cache.cjs`,
23+
}
24+
25+
// set config
26+
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(runtimeConfig.config)
3727
}

0 commit comments

Comments
 (0)