@modern-js/main-doc 2.54.6 → 2.56.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. package/docs/en/apis/app/hooks/src/entry.mdx +42 -0
  2. package/docs/en/apis/app/hooks/src/entry.server.mdx +52 -0
  3. package/docs/en/apis/app/hooks/src/index_.mdx +4 -0
  4. package/docs/en/apis/app/runtime/core/bootstrap.mdx +4 -0
  5. package/docs/en/apis/app/runtime/core/create-app.mdx +5 -1
  6. package/docs/en/apis/app/runtime/core/create-root.mdx +22 -0
  7. package/docs/en/apis/app/runtime/core/render.mdx +42 -0
  8. package/docs/en/apis/app/runtime/ssr/renderStreaming.mdx +69 -0
  9. package/docs/en/apis/app/runtime/ssr/renderString.mdx +62 -0
  10. package/docs/en/apis/app/runtime/ssr/requestHandler.mdx +47 -0
  11. package/docs/en/components/debug-app.mdx +3 -3
  12. package/docs/en/configure/app/dev/live-reload.mdx +27 -0
  13. package/docs/en/configure/app/dev/setup-middlewares.mdx +69 -0
  14. package/docs/en/configure/app/dev/watch-files.mdx +59 -0
  15. package/docs/en/configure/app/dev/write-to-disk.mdx +38 -0
  16. package/docs/en/configure/app/output/ssg.mdx +4 -0
  17. package/docs/en/configure/app/source/enable-async-entry.mdx +4 -4
  18. package/docs/en/configure/app/source/enable-custom-entry.mdx +41 -0
  19. package/docs/en/guides/advanced-features/ssr/cache.mdx +7 -2
  20. package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -1
  21. package/docs/en/guides/basic-features/routes.mdx +1 -1
  22. package/docs/en/guides/concept/entries.mdx +50 -36
  23. package/docs/en/guides/get-started/quick-start.mdx +24 -8
  24. package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +30 -177
  25. package/docs/zh/apis/app/hooks/src/entry.mdx +42 -0
  26. package/docs/zh/apis/app/hooks/src/entry.server.mdx +52 -0
  27. package/docs/zh/apis/app/hooks/src/index_.mdx +4 -0
  28. package/docs/zh/apis/app/runtime/core/bootstrap.mdx +4 -0
  29. package/docs/zh/apis/app/runtime/core/create-app.mdx +5 -1
  30. package/docs/zh/apis/app/runtime/core/create-root.mdx +22 -0
  31. package/docs/zh/apis/app/runtime/core/render.mdx +43 -0
  32. package/docs/zh/apis/app/runtime/ssr/renderStreaming.mdx +69 -0
  33. package/docs/zh/apis/app/runtime/ssr/renderString.mdx +62 -0
  34. package/docs/zh/apis/app/runtime/ssr/requestHandler.mdx +47 -0
  35. package/docs/zh/components/debug-app.mdx +3 -2
  36. package/docs/zh/configure/app/dev/client.mdx +1 -1
  37. package/docs/zh/configure/app/dev/live-reload.mdx +27 -0
  38. package/docs/zh/configure/app/dev/setup-middlewares.mdx +69 -0
  39. package/docs/zh/configure/app/dev/watch-files.mdx +59 -0
  40. package/docs/zh/configure/app/dev/write-to-disk.mdx +38 -0
  41. package/docs/zh/configure/app/output/ssg.mdx +4 -0
  42. package/docs/zh/configure/app/source/enable-async-entry.mdx +4 -4
  43. package/docs/zh/configure/app/source/enable-custom-entry.mdx +41 -0
  44. package/docs/zh/guides/advanced-features/ssr/cache.mdx +6 -2
  45. package/docs/zh/guides/basic-features/data/data-fetch.mdx +1 -1
  46. package/docs/zh/guides/basic-features/routes.mdx +1 -1
  47. package/docs/zh/guides/concept/entries.mdx +54 -41
  48. package/docs/zh/guides/get-started/quick-start.mdx +26 -10
  49. package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +29 -174
  50. package/package.json +7 -7
  51. package/docs/en/apis/app/hooks/src/pages.mdx +0 -186
  52. package/docs/zh/apis/app/hooks/src/pages.mdx +0 -187
  53. package/docs/zh/apis/monorepo/commands/deploy.mdx +0 -38
@@ -0,0 +1,42 @@
1
+ ---
2
+ title: entry.[tj]s
3
+ sidebar_position: 4
4
+ ---
5
+ # entry.[tj]s
6
+
7
+ :::info
8
+ Using this file requires enabling [source.enableCustomEntry](/configure/app/source/enable-custom-entry).
9
+ :::
10
+
11
+ Normally, the [`routes/`](/apis/app/hooks/src/routes.html) and [`App.[tj]sx`](/apis/app/hooks/src/app) hook files can meet our needs. When we need to add custom behavior before component rendering or take full control of the webpack packaging entry, we can create `entry.[tj]s` file in the src or entry directory. Here are two cases for discussion:
12
+
13
+ ## Add custom behavior before Render
14
+
15
+ This is implemented in `src/entry.[tj]s` as follows:
16
+
17
+ ```js title=src/entry.tsx
18
+ import { createRoot } from '@modern-js/runtime/react';
19
+ import { render } from '@modern-js/runtime/browser';
20
+
21
+ const ModernRoot = createRoot();
22
+
23
+ async function beforeRender() {
24
+ // todo
25
+ }
26
+
27
+ beforeRender().then(() => {
28
+ render(<ModernRoot />);
29
+ });
30
+ ```
31
+
32
+ ## Take full control of the webpack entry
33
+
34
+ When the project does not install the `@modern-js/runtime` dependency, `src/entry.[tj]sx?` is the real webpack packaging entry file, and you can directly organize the code like using create-react-app and other scaffolds:
35
+
36
+ ```js title=src/entry.jsx
37
+ import React from 'react';
38
+ import ReactDOM from 'react-dom/client';
39
+ import App from './App';
40
+
41
+ ReactDOM.createRoot(document.getElementById('root')!).render(<App />);
42
+ ```
@@ -0,0 +1,52 @@
1
+ ---
2
+ title: entry.server.[tj]sx
3
+ sidebar_position: 5
4
+ ---
5
+
6
+ # entry.server.[tj]sx
7
+
8
+ When the project initiates `server.ssr`, Modern.js generates a default Server-Side entry. The sample code is as follows:
9
+
10
+ ```tsx title="entry.server.tsx"
11
+ import {
12
+ renderString,
13
+ createRequestHandler,
14
+ } from '@modern-js/runtime/ssr/server';
15
+
16
+ const handleRequest = async (request, ServerRoot, options) => {
17
+ const body = await renderString(request, <ServerRoot />, options);
18
+
19
+ return new Response(body, {
20
+ headers: {
21
+ 'content-type': 'text/html; charset=utf-8',
22
+ },
23
+ });
24
+ };
25
+
26
+ export default createRequestHandler(handleRequest);
27
+ ```
28
+
29
+ ## Add Custom Entry Points
30
+
31
+ Users need to customize the Server-Side Rendering entry points, they can customize the server entry in `src/entry.server.ts` or `src/{entryName}/entry.server.ts`.
32
+
33
+ ```tsx title="src/entry.server.tsx"
34
+ import { renderString, createRequestHandler } from '@edenx/runtime/ssr/server';
35
+ import type { HandleRequest } from '@edenx/runtime/ssr/server';
36
+
37
+ const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
38
+ // do something before rendering
39
+ const body = await renderString(request, <ServerRoot />, options);
40
+
41
+ const newBody = body + '<div>Byte-Dance</div>';
42
+
43
+ return new Response(newBody, {
44
+ headers: {
45
+ 'content-type': 'text/html; charset=UTF-8',
46
+ 'x-custom-header': 'abc',
47
+ },
48
+ });
49
+ };
50
+
51
+ export default createRequestHandler(handleRequest);
52
+ ```
@@ -4,6 +4,10 @@ sidebar_position: 4
4
4
  ---
5
5
  # index.[tj]s
6
6
 
7
+ :::warning
8
+ Soon to be deprecated, it is recommended to use [`entry.[tj]s`](/apis/app/hooks/src/entry)。
9
+ :::
10
+
7
11
  The identifier for the entry point when the application uses custom `bootstrap`.
8
12
 
9
13
  In general, the [`App.[tj]sx`](/apis/app/hooks/src/app) hook file can already meet our needs. When we need to add custom behavior before `bootstrap` or completely take over the webpack packaging entry, we can place `index.[tj]s` in the `src` or entry directory. There are two cases below:
@@ -3,6 +3,10 @@ title: bootstrap
3
3
  ---
4
4
  # bootstrap
5
5
 
6
+ :::warning
7
+ Soon to be deprecated, it is recommended to use [`render`](/apis/app/runtime/core/render).
8
+ :::
9
+
6
10
  Used to start and mount App, usually not manually called. This API is only required when using [Custom Bootstrap](/guides/concept/entries#custom-bootstrap).
7
11
 
8
12
  ## Usage
@@ -3,7 +3,11 @@ title: createApp
3
3
  ---
4
4
  # createApp
5
5
 
6
- Used to create custom entries, custom runtime plugins. This API is only required when using [Custom App](/guides/concept/entries#app).
6
+ :::warning
7
+ Soon to be deprecated, it is recommended to use [`createRoot`](/apis/app/runtime/core/create-root).
8
+ :::
9
+
10
+ Used for creating custom entries and customizing runtime plugins.
7
11
 
8
12
  ## Usage
9
13
 
@@ -0,0 +1,22 @@
1
+ ---
2
+ title: createRoot
3
+ ---
4
+ # createRoot
5
+
6
+ It is used to create the root component provided by Modern.js, which will automatically register Runtime plugins and complete the initialization of Runtime plugins.
7
+
8
+ ## Usage
9
+
10
+ ```ts
11
+ import { createRoot } from '@modern-js/runtime/react';
12
+ ```
13
+
14
+ ## Function Signature
15
+
16
+ ```ts
17
+ export function createRoot(UserApp?: React.ComponentType | null): React.ComponentType<any>;
18
+ ```
19
+
20
+ ### Parameters
21
+
22
+ - `UserApp`: an optional parameter, and the default is the component exported from `App.tsx`.
@@ -0,0 +1,42 @@
1
+ ---
2
+ title: render
3
+ ---
4
+ # render
5
+
6
+ It is used to render project components.
7
+
8
+ ## Usage
9
+
10
+ ```ts
11
+ import { render } from '@modern-js/runtime/browser';
12
+
13
+ render(<ModernRoot />);
14
+ ```
15
+
16
+ ## Function Signature
17
+
18
+ ```ts
19
+ export function render(App: React.ReactElement, id?: HTMLElement | string): Promise<any>;
20
+ ```
21
+
22
+ ### Parameters
23
+
24
+ - `App`:An instance of ReactElement created through [`createRoot`](./create-root).
25
+ - `id`:The id of the DOM root element to be mounted, such as "root".
26
+
27
+ ## Example
28
+
29
+ ```tsx
30
+ import { createRoot } from '@modern-js/runtime/react';
31
+ import { render } from '@modern-js/runtime/browser';
32
+
33
+ const ModernRoot = createRoot();
34
+
35
+ async function beforeRender() {
36
+ // todo
37
+ }
38
+
39
+ beforeRender().then(() => {
40
+ render(<ModernRoot />);
41
+ });
42
+ ```
@@ -0,0 +1,69 @@
1
+ ---
2
+ title: renderStreaming
3
+ ---
4
+
5
+ # renderStreaming
6
+
7
+ Used for React v18+ Streaming SSR to render readable streams, used in conjunction with `createRequestHandler`.
8
+
9
+ ## Usage
10
+
11
+ ```ts title="src/entry.server.tsx"
12
+ import {
13
+ renderStreaming,
14
+ createRequestHandler,
15
+ } from '@modern-js/runtime/ssr/server';
16
+
17
+ const handleRequest = async (request, ServerRoot, options) => {
18
+ const stream = await renderStreaming(request, <ServerRoot />, options);
19
+
20
+ return new Response(stream, {
21
+ headers: {
22
+ 'content-type': 'text/html; charset=utf-8',
23
+ },
24
+ });
25
+ };
26
+
27
+ export default createRequestHandler(handleRequest);
28
+ ```
29
+
30
+ ## Function Signature
31
+
32
+ ```ts
33
+ export type RenderStreaming = (
34
+ request: Request,
35
+ serverRoot: React.ReactElement,
36
+ optinos: RenderOptions,
37
+ ) => Promise<ReadableStream>;
38
+ ```
39
+
40
+ ## Example
41
+
42
+ ```tsx title="src/entry.server.tsx"
43
+ import {
44
+ renderStreaming,
45
+ createRequestHandler,
46
+ } from '@modern-js/runtime/ssr/server';
47
+
48
+ const handleRequest = async (request, ServerRoot, options) => {
49
+ // do something before render
50
+ const stream = await renderStreaming(request, <ServerRoot />, options);
51
+
52
+ // docs: https://developer.mozilla.org/en-US/docs/Web/API/TransformStream
53
+ const transformStream = new TransformStream({
54
+ transform(chunk, controller) {
55
+ // do some transform
56
+ },
57
+ });
58
+
59
+ stream.pipeThrough(transformStream);
60
+
61
+ return new Response(transformStream.readable, {
62
+ headers: {
63
+ 'content-type': 'text/html; charset=utf-8',
64
+ },
65
+ });
66
+ };
67
+
68
+ export default createRequestHandler(handleRequest);
69
+ ```
@@ -0,0 +1,62 @@
1
+ ---
2
+ title: renderString
3
+ ---
4
+
5
+ # renderString
6
+
7
+ Used for React String SSR to render strings, used in conjunction with `createRequestHandler`.
8
+
9
+ ## Usage
10
+
11
+ ```tsx title="src/entry.server.tsx"
12
+ import {
13
+ renderString,
14
+ createRequestHandler,
15
+ } from '@modern-js/runtime/ssr/server';
16
+
17
+ const handleRequest = async (request, ServerRoot, options) => {
18
+ const body = await renderString(request, <ServerRoot />, options);
19
+
20
+ return new Response(body, {
21
+ headers: {
22
+ 'content-type': 'text/html; charset=utf-8',
23
+ },
24
+ });
25
+ };
26
+
27
+ export default createRequestHandler(handleRequest);
28
+ ```
29
+
30
+ ## Function Signature
31
+
32
+ ```ts
33
+ export type RenderString = (
34
+ request: Request,
35
+ serverRoot: React.ReactElement,
36
+ optinos: RenderOptions,
37
+ ) => Promise<string>;
38
+ ```
39
+
40
+ ## Example
41
+
42
+ ```tsx title="src/entry.server.tsx"
43
+ import {
44
+ renderString,
45
+ createRequestHandler,
46
+ } from '@modern-js/runtime/ssr/server';
47
+
48
+ const handleRequest = async (request, ServerRoot, options) => {
49
+ // do something before render
50
+ const body = await renderString(request, <ServerRoot />, options);
51
+
52
+ const newBody = body + '<div>Byte-Dance</div>';
53
+
54
+ return new Response(newBody, {
55
+ headers: {
56
+ 'content-type': 'text/html; charset=utf-8',
57
+ },
58
+ });
59
+ };
60
+
61
+ export default createRequestHandler(handleRequest);
62
+ ```
@@ -0,0 +1,47 @@
1
+ ---
2
+ title: createRequestHandler
3
+ ---
4
+
5
+ # createRequestHandler
6
+
7
+ Used to customize the Server-Side Rendering entry to return the `requestHandler`.
8
+
9
+ ## Usage
10
+
11
+ ```tsx title="src/entry.server.tsx"
12
+ import {
13
+ renderString,
14
+ createRequestHandler,
15
+ } from '@modern-js/runtime/ssr/server';
16
+
17
+ const handleRequest = async (request, ServerRoot, options) => {
18
+ const body = await renderString(request, <ServerRoot />, options);
19
+
20
+ return new Response(body, {
21
+ headers: {
22
+ 'content-type': 'text/html; charset=utf-8',
23
+ },
24
+ });
25
+ };
26
+
27
+ export default createRequestHandler(handleRequest);
28
+ ```
29
+
30
+ ## Function Signature
31
+
32
+ ```ts
33
+ export type HandleRequest = (
34
+ request: Request,
35
+ ServerRoot: React.ComponentType,
36
+ options: HandleRequestOptions,
37
+ ) => Promise<Response>;
38
+
39
+ export type RequestHandler = (
40
+ request: Request,
41
+ options: RequestHandlerOptions,
42
+ ) => Promise<Response>;
43
+
44
+ export type CreateRequestHandler = (
45
+ handleRequest: HandleRequest,
46
+ ) => Promise<RequestHandler>;
47
+ ```
@@ -2,11 +2,11 @@ Run `pnpm run dev` in the project to start the project:
2
2
 
3
3
  ```bash
4
4
  $ pnpm run dev
5
-
6
5
  > modern dev
7
6
 
8
- info Starting dev server...
9
- ready Client compiled in 50 ms
7
+ Modern.js Framework v2.55.0
8
+
9
+ ready Client compiled in 0.86 s
10
10
 
11
11
  > Local: http://localhost:8080/
12
12
  > Network: http://192.168.0.1:8080/
@@ -0,0 +1,27 @@
1
+ ---
2
+ sidebar_label: liveReload
3
+ ---
4
+
5
+ # dev.liveReload
6
+
7
+ - **Type:** `boolean`
8
+ - **Default:** `true`
9
+
10
+ Whether to reload the page when source files are changed.
11
+
12
+ By default, Modern.js uses HMR as the preferred method to update modules. If HMR is disabled or cannot be used in certain scenarios, it will automatically fallback to liveReload.
13
+
14
+ Please refer to [Hot Module Replacement](https://rsbuild.dev/guide/advanced/hmr) for more information.
15
+
16
+ ## Disabling liveReload
17
+
18
+ If you need to disable liveReload, you can set both `dev.hmr` and `dev.liveReload` to `false`. Then, no Web Socket requests will be made to the dev server on the page, and the page will not automatically refresh when file change.
19
+
20
+ ```js
21
+ export default {
22
+ dev: {
23
+ hmr: false,
24
+ liveReload: false,
25
+ },
26
+ };
27
+ ```
@@ -0,0 +1,69 @@
1
+ ---
2
+ sidebar_label: setupMiddlewares
3
+ ---
4
+
5
+ # dev.setupMiddlewares
6
+
7
+ - **Type:**
8
+
9
+ ```ts
10
+ type ServerAPIs = {
11
+ sockWrite: (
12
+ type: string,
13
+ data?: string | boolean | Record<string, any>,
14
+ ) => void;
15
+ };
16
+
17
+ type SetupMiddlewares = Array<
18
+ (
19
+ middlewares: {
20
+ unshift: (...handlers: RequestHandler[]) => void;
21
+ push: (...handlers: RequestHandler[]) => void;
22
+ },
23
+ server: ServerAPIs,
24
+ ) => void
25
+ >;
26
+ ```
27
+
28
+ - **Default:** `undefined`
29
+
30
+ Provides the ability to execute a custom function and apply custom middlewares.
31
+
32
+ The order among several different types of middleware is: `unshift` => internal middlewares => `push`.
33
+
34
+ ```js
35
+ export default {
36
+ dev: {
37
+ setupMiddlewares: [
38
+ (middlewares, server) => {
39
+ middlewares.unshift((req, res, next) => {
40
+ next();
41
+ });
42
+
43
+ middlewares.push((req, res, next) => {
44
+ next();
45
+ });
46
+ },
47
+ ],
48
+ },
49
+ };
50
+ ```
51
+
52
+ It is possible to use some server api to meet special scenario requirements:
53
+
54
+ - sockWrite. Allow send some message to HMR client, and then the HMR client will take different actions depending on the message type. If you send a "content changed" message, the page will reload.
55
+
56
+ ```js
57
+ export default {
58
+ dev: {
59
+ setupMiddlewares: [
60
+ (middlewares, server) => {
61
+ // add custom watch & trigger page reload when change
62
+ watcher.on('change', (changed) => {
63
+ server.sockWrite('content-changed');
64
+ });
65
+ },
66
+ ],
67
+ },
68
+ };
69
+ ```
@@ -0,0 +1,59 @@
1
+ ---
2
+ sidebar_label: watchFiles
3
+ ---
4
+
5
+ # dev.watchFiles
6
+
7
+ - **Type:**
8
+
9
+ ```ts
10
+ type WatchFiles = {
11
+ paths: string | string[];
12
+ // watch options for chokidar
13
+ options?: WatchOptions;
14
+ };
15
+ ```
16
+
17
+ - **Default:** `undefined`
18
+
19
+ Watch files and directories for changes. When a file changes, the page will be reloaded.
20
+
21
+ If both `dev.hmr` and `dev.liveReload` are set to false, `watchFiles` will be ignored.
22
+
23
+ :::tip
24
+ When files in WatchFiles change, reloading and recompilation of the configuration file will not be triggered.
25
+ :::
26
+
27
+ ### Example
28
+
29
+ You can configure a list of globs/directories/files to watch for file changes.
30
+
31
+ ```js
32
+ export default {
33
+ dev: {
34
+ watchFiles: {
35
+ // watch a single file
36
+ paths: 'public/demo.txt',
37
+ // use a glob pattern
38
+ paths: 'src/**/*.txt',
39
+ // watch multiple file paths
40
+ paths: ['src/**/*.txt', 'public/**/*'],
41
+ },
42
+ },
43
+ };
44
+ ```
45
+
46
+ You can also specify [chokidar](https://github.com/paulmillr/chokidar#api) watcher options by passing an object with `paths` and `options` properties.
47
+
48
+ ```js
49
+ export default {
50
+ dev: {
51
+ watchFiles: {
52
+ paths: 'src/**/*.txt',
53
+ options: {
54
+ usePolling: false,
55
+ },
56
+ },
57
+ },
58
+ };
59
+ ```
@@ -0,0 +1,38 @@
1
+ ---
2
+ sidebar_label: writeToDisk
3
+ ---
4
+
5
+ # dev.writeToDisk
6
+
7
+ - **Type:** `boolean | ((filename: string) => boolean)`
8
+ - **Default:** `(file) => !file.includes('.hot-update.')`
9
+
10
+ Used to control whether the build artifacts of the development environment are written to the disk.
11
+
12
+ ## Writing to Memory
13
+
14
+ You can choose to save the build artifacts in the memory of the dev server, thereby reducing the overhead caused by file operations.
15
+
16
+ Simply set the `dev.writeToDisk` option to `false`:
17
+
18
+ ```ts
19
+ export default {
20
+ dev: {
21
+ writeToDisk: false,
22
+ },
23
+ };
24
+ ```
25
+
26
+ ## Matching Specific Files
27
+
28
+ You can also set `dev.writeToDisk` to a function to match only certain files. When the function returns `false`, the file will not be written; when it returns `true`, the file will be written to disk.
29
+
30
+ For example, Modern.js by default write files to disk while excluding hot-update temporary files:
31
+
32
+ ```ts
33
+ export default {
34
+ dev: {
35
+ writeToDisk: (file) => !file.includes('.hot-update.'),
36
+ },
37
+ };
38
+ ```
@@ -9,6 +9,10 @@ sidebar_label: ssg
9
9
 
10
10
  Enable the SSG for **Self-controlled Routing** or **Conventional Routing**.
11
11
 
12
+ :::info Enable SSG
13
+ This configuration will only be available when the SSG feature is enabled. Please read the [Static Site Generation](/guides/advanced-features/ssg) documentation to learn how to enable the SSG feature.
14
+ :::
15
+
12
16
  :::info
13
17
  For more routes detail, see [Routing](/guides/basic-features/routes).
14
18
 
@@ -35,15 +35,15 @@ Then run the `dev` or `build` command, and you will see that the files automatic
35
35
  node_modules
36
36
  └─ .modern-js
37
37
  └─ main
38
- ├─ bootstrap.jsx # real entry code
39
- ├─ index.js # asynchronous entry file
38
+ ├─ bootstrap.jsx # asynchronous entry file
39
+ ├─ index.js # real entry code
40
40
  └─ index.html
41
41
  ```
42
42
 
43
- The contents of `index.js` are as follows:
43
+ The contents of `bootstrap.js` are as follows:
44
44
 
45
45
  ```js
46
- import('./bootstrap.jsx');
46
+ import('./index.jsx');
47
47
  ```
48
48
 
49
49
  At this point, you can consume any remote module in the current page.
@@ -0,0 +1,41 @@
1
+ ---
2
+ sidebar_label: enableCustomEntry
3
+ ---
4
+
5
+ # source.enableCustomEntry
6
+
7
+ - **Type:** `boolean`
8
+ - **Default:** `false`
9
+
10
+ This option is used for custom Modern.js entries.
11
+
12
+ When this option is enabled, Modern.js will use the `src/entry.[jt]sx` file as the project's entry point. For more details, refer to [Entries](/guides/concept/entries.html).
13
+
14
+ ## Example
15
+
16
+ First, enable this option in the configuration file:
17
+
18
+ ```ts title="modern.config.ts"
19
+ export default defineConfig({
20
+ source: {
21
+ enableCustomEntry: true,
22
+ },
23
+ });
24
+ ```
25
+
26
+ Create the `src/entry.tsx` file:
27
+
28
+ ```tsx
29
+ import { createRoot } from '@modern-js/runtime/react';
30
+ import { render } from '@modern-js/runtime/browser';
31
+
32
+ const ModernRoot = createRoot();
33
+
34
+ async function beforeRender() {
35
+ // todo
36
+ }
37
+
38
+ beforeRender().then(() => {
39
+ render(<ModernRoot />);
40
+ });
41
+ ```
@@ -53,10 +53,10 @@ In this, customKey is used for custom cache key. By default, Modern.js will use
53
53
  ```ts
54
54
  export type CacheOptionProvider = (
55
55
  req: IncomingMessage,
56
- ) => Promise<CacheControl> | CacheControl;
56
+ ) => Promise<CacheControl | false> | CacheControl | false;
57
57
  ```
58
58
 
59
- Sometimes developers need to customise the cache key through req, which can be handled using the function form, as shown in the following code:
59
+ Sometimes developers need to customise the cache key through req or when caching does not work for specific URLs, which can be handled using the function form, as shown in the following code:
60
60
 
61
61
  ```ts title="server/cache.ts"
62
62
  import type { CacheOption, CacheOptionProvider } from '@modern-js/runtime/server';
@@ -66,6 +66,11 @@ const provider: CacheOptionProvider = (req) => {
66
66
 
67
67
  const key = computedKey(url, headers, ...);
68
68
 
69
+ if(url.includes('no-cache=1')) {
70
+ return false;
71
+ }
72
+
73
+
69
74
  return {
70
75
  maxAge: 500, // ms
71
76
  staleWhileRevalidate: 1000, // ms