Skip to content

Commit 0e53d83

Browse files
use wrangler aliases for fs calls
1 parent cb9ee90 commit 0e53d83

File tree

4 files changed

+11
-20
lines changed

4 files changed

+11
-20
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// In our aliased fs code: apps/site/.cloudflare/node/fs/promises.mjs we are importing `getCloudflareContext`
2+
// from `@opennextjs/cloudflare`, this in turn imports from `server-only`, this aliasing maxes it so that
3+
// server-only is not actually removed from the final bundle as it should causing an incorrect server internal
4+
// error, so here we're also mocking out the server-only module as well

apps/site/CLOUDFLARE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ The following is an incomplete list of tasks and problems that still need to be
7777
- [ ] we need to ensure that we don't end up accidentally downloading this big file to the client as part of hydration
7878
- [x] once we have easy access to the list of files, we should roll back changes to `next-data/providers/blogData.ts`
7979
- [ ] back out most changes from `next.dynamic.mjs`
80-
- [ ] instead of using runtime detection via `globalThis.navigator?.userAgent`, we should instead use `alias` feature in `wrangler.toml` to override the implementation of `node:fs` calls but only when running in workerd as we need the build to keep on running in node.js for SSG to work
80+
- [x] instead of using runtime detection via `globalThis.navigator?.userAgent`, we should instead use `alias` feature in `wrangler.toml` to override the implementation of `node:fs` calls but only when running in workerd as we need the build to keep on running in node.js for SSG to work
8181
- [ ] could we reimplement the `existsAsync` call as sync `exists` which consults `getMarkdownFiles` from the task above? alternatively
8282
- [ ] properly implement the rest of `.cloudflare/node/*` polyfills
8383
- [ ] remove symlink hack in `package.json#build:cloudflare`

apps/site/next.dynamic.mjs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict';
22

3-
import { exists as nodeExists } from 'node:fs';
4-
import { readFile as nodeReadFile } from 'node:fs/promises';
3+
import { exists } from 'node:fs';
4+
import { readFile } from 'node:fs/promises';
55
import { join, normalize, sep } from 'node:path';
66

77
import matter from 'gray-matter';
88
import { cache } from 'react';
99
import { VFile } from 'vfile';
1010

11-
import { readFile as runtimeReadFile } from './.cloudflare/node/fs/promises.mjs';
12-
import { exists as runtimeExists } from './.cloudflare/node/fs.mjs';
1311
import { getMarkdownFiles } from './.next.helpers.mjs';
1412
import { BASE_URL, BASE_PATH, IS_DEVELOPMENT } from './next.constants.mjs';
1513
import {
@@ -21,15 +19,6 @@ import { siteConfig } from './next.json.mjs';
2119
import { availableLocaleCodes, defaultLocale } from './next.locales.mjs';
2220
import { compileMDX } from './next.mdx.compiler.mjs';
2321

24-
const readFile =
25-
globalThis.navigator?.userAgent === 'Cloudflare-Workers'
26-
? runtimeReadFile
27-
: nodeReadFile;
28-
const exists =
29-
globalThis.navigator?.userAgent === 'Cloudflare-Workers'
30-
? runtimeExists
31-
: nodeExists;
32-
3322
// This is the combination of the Application Base URL and Base PATH
3423
const baseUrlAndPath = `${BASE_URL}${BASE_PATH}`;
3524

@@ -50,7 +39,7 @@ const createCachedMarkdownCache = () => {
5039
if (IS_DEVELOPMENT) {
5140
return {
5241
has: () => false,
53-
set: () => {},
42+
set: () => { },
5443
get: () => null,
5544
};
5645
}

apps/site/wrangler.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ enabled = true
99
head_sampling_rate = 1
1010

1111
[alias]
12-
# "node:fs" = "./.cloudflare/node/fs.mjs"
13-
# "node:fs/promises" = "./.cloudflare/node/fs/promises.mjs"
14-
# "node:fs" = "./.cloudflare/node/fs.mjs"
15-
# "node:fs/promises" = "./.cloudflare/node/fs/promises.mjs"
16-
12+
"node:fs" = "./.cloudflare/node/fs.mjs"
13+
"node:fs/promises" = "./.cloudflare/node/fs/promises.mjs"
14+
"server-only" = "./.cloudflare/server-only.mjs"
1715

1816
[assets]
1917
directory = ".open-next/assets"

0 commit comments

Comments
 (0)