Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ playground/
.cursor
.claude
AGENTS.md
.specify/
12 changes: 6 additions & 6 deletions packages/cli/plugin-bff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"jsnext:source": "./src/cli.ts",
"default": "./dist/cjs/cli.js"
},
"./server": {
"./server-plugin": {
"types": "./dist/types/server.d.ts",
"jsnext:source": "./src/server.ts",
"default": "./dist/cjs/server.js"
Expand All @@ -42,12 +42,12 @@
"jsnext:source": "./src/loader.ts",
"default": "./dist/cjs/loader.js"
},
"./hono": {
"./server": {
"types": "./dist/types/runtime/hono/index.d.ts",
"jsnext:source": "./src/runtime/hono/index.ts",
"default": "./dist/cjs/runtime/hono/index.js"
},
"./runtime/create-request": {
"./client": {
"types": "./dist/types/create-request/index.d.ts",
"jsnext:source": "./src/runtime/create-request/index.ts",
"default": "./dist/cjs/runtime/create-request/index.js"
Expand All @@ -61,13 +61,13 @@
"cli": [
"./dist/types/cli.d.ts"
],
"server": [
"server-plugin": [
"./dist/types/server.d.ts"
],
"hono": [
"server": [
"./dist/types/runtime/hono/index.d.ts"
],
"runtime/create-request": [
"client": [
"./dist/types/runtime/create-request/index.d.ts"
]
}
Expand Down
13 changes: 3 additions & 10 deletions packages/cli/plugin-bff/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import runtimeGenerator from './utils/runtimeGenerator';

const DEFAULT_API_PREFIX = '/api';
const TS_CONFIG_FILENAME = 'tsconfig.json';
const RUNTIME_CREATE_REQUEST = '@modern-js/plugin-bff/runtime/create-request';
const RUNTIME_HONO = '@modern-js/plugin-bff/hono';
const RUNTIME_CREATE_REQUEST = '@modern-js/plugin-bff/client';
const RUNTIME_HONO = '@modern-js/plugin-bff/server';

export const bffPlugin = (): CliPlugin<AppTools> => ({
name: '@modern-js/plugin-bff',
Expand Down Expand Up @@ -230,13 +230,6 @@ export const bffPlugin = (): CliPlugin<AppTools> => ({
requestCreator: (bff as any)?.requestCreator,
httpMethodDecider,
});

chain.resolve.alias.set('@api', apiDirectory);

chain.resolve.alias.set(
'@modern-js/runtime/bff',
RUNTIME_CREATE_REQUEST,
);
},
},
output: {
Expand Down Expand Up @@ -284,7 +277,7 @@ export const bffPlugin = (): CliPlugin<AppTools> => ({

api._internalServerPlugins(({ plugins }) => {
plugins.push({
name: '@modern-js/plugin-bff/server',
name: '@modern-js/plugin-bff/server-plugin',
});
return { plugins };
});
Expand Down
15 changes: 0 additions & 15 deletions packages/cli/plugin-bff/src/helper.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/cli/plugin-bff/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,11 @@ export default (): ServerPlugin => ({
prefix,
httpMethodDecider,
});
const apiMode = apiRouter.getApiMode();

const apiHandlerInfos = await apiRouter.getApiHandlers();
api.updateServerContext({
...appContext,
apiRouter,
apiHandlerInfos,
apiMode,
});
return next(input);
}) as any);
Expand Down
32 changes: 30 additions & 2 deletions packages/cli/plugin-bff/src/utils/runtimeGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
import path from 'path';
import { fs } from '@modern-js/utils';

/**
* Get package name from package.json file
* @param appDirectory - Application directory path
* @returns Package name or undefined if not found
*/
const getPackageName = (appDirectory: string): string | undefined => {
try {
const packageJsonPath = path.resolve(appDirectory, './package.json');
const packageJson = require(packageJsonPath);
return packageJson.name;
} catch (error) {
// If package.json doesn't exist or is invalid, return undefined
return undefined;
}
};

async function runtimeGenerator({
runtime,
appDirectory,
relativeDistPath,
}: { runtime: string; appDirectory: string; relativeDistPath: string }) {
packageName,
}: {
runtime: string;
appDirectory: string;
relativeDistPath: string;
packageName?: string;
}) {
const pluginDir = path.resolve(
appDirectory,
`./${relativeDistPath}`,
'runtime',
);

const requestId =
packageName ||
getPackageName(appDirectory) ||
process.env.npm_package_name ||
'default';

const source = `import { configure as _configure } from '${runtime}'
const configure = (options) => {
return _configure({
...options,
requestId: '${process.env.npm_package_name}',
requestId: '${requestId}',
});
}
export { configure }
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/plugin-bff/tests/__snapshots__/server.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
exports[`bff server plugin prepareApiServer should work well 1`] = `
[
{
"filename": "<ROOT>/fixtures/function/api/hello.ts",
"filename": "<ROOT>/fixtures/function/api/lambda/hello.ts",
"handler": [Function],
"httpMethod": "GET",
"name": "get",
"routeName": "/hello",
"routePath": "/hello",
},
{
"filename": "<ROOT>/fixtures/function/api/upload.ts",
"filename": "<ROOT>/fixtures/function/api/lambda/upload.ts",
"handler": [Function],
"httpMethod": "GET",
"name": "get",
"routeName": "/upload",
"routePath": "/upload",
},
{
"filename": "<ROOT>/fixtures/function/api/user/[id].ts",
"filename": "<ROOT>/fixtures/function/api/lambda/user/[id].ts",
"handler": [Function],
"httpMethod": "GET",
"name": "get",
Expand All @@ -32,23 +32,23 @@ exports[`bff server plugin prepareApiServer should work well 1`] = `
exports[`bff server plugin prepareApiServer should work well with prefix 1`] = `
[
{
"filename": "<ROOT>/fixtures/function/api/hello.ts",
"filename": "<ROOT>/fixtures/function/api/lambda/hello.ts",
"handler": [Function],
"httpMethod": "GET",
"name": "get",
"routeName": "/hello",
"routePath": "/api/hello",
},
{
"filename": "<ROOT>/fixtures/function/api/upload.ts",
"filename": "<ROOT>/fixtures/function/api/lambda/upload.ts",
"handler": [Function],
"httpMethod": "GET",
"name": "get",
"routeName": "/upload",
"routePath": "/api/upload",
},
{
"filename": "<ROOT>/fixtures/function/api/user/[id].ts",
"filename": "<ROOT>/fixtures/function/api/lambda/user/[id].ts",
"handler": [Function],
"httpMethod": "GET",
"name": "get",
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/plugin-bff/types.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Used to obtain Hono context in an integrated BFF function.
## Usage

```ts
import { useHonoContext } from '@modern-js/plugin-bff/hono';
import { useHonoContext } from '@modern-js/plugin-bff/server';
```

## Function Signature
Expand All @@ -20,7 +20,7 @@ import { useHonoContext } from '@modern-js/plugin-bff/hono';
Developers can use `context` to obtain more request information, such as setting response headers:

```ts
import { useHonoContext } from '@modern-js/plugin-bff/hono';
import { useHonoContext } from '@modern-js/plugin-bff/server';

export async function get() {
const c = useHonoContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ When the value type is `Object`, the following properties can be configured:
| ---------------- | ------------------------------------------------------------- | -------------------------------------- | ---------------------------------------------------------------------------- |
| mode | `string` | `string` | which defaults to using `renderToString` for rendering. Configure `stream` to enable streaming rendering |
| forceCSR | `boolean` | `false` | which is off by default for forcing CSR rendering. Configure `true` to force CSR by adding `?csr=true` or adding `x-modern-ssr-fallback` header when accessing the page |
| disablePrerender | `boolean` | `fasle` | To ensure compatibility with the old data request method (`useLoader`), by default, Modern.js performs pre-rendering of components.However, if developers want to reduce one rendering when there is no use of the useLoader API in your project, you can set the configuration `disablePrerender=true` |
| unsafeHeaders | `string[]` | `[]` | For safety reasons, Modern.js does not add excessive content to SSR_DATA. Developers can use this configuration to specify the headers that need to be injected |
| loaderFailureMode| `clientRender \| errorBoundary` | `errorBoundary` | The default configuration is `'errorBoundary'`, when an error occurs in [data loader](/en/guides/basic-features/data/data-fetch.html#data-loader-recommended), it will default to rendering the [`Error`](/en/guides/basic-features/routes.html#errorboundary) component of the route. When configured as `'clientRender'`, if a loader throws an error, it switch to client-side rendering,you can use it with [Client Loader](/en/guides/basic-features/data/data-fetch.html#client-loader) |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The `configure` function from `${package_name}/runtime` supports domain configur

```ts title="src/routes/page.tsx"
import { configure } from '${package_name}/runtime';
import { configure as innerConfigure } from '@modern-js/runtime/bff';
import { configure as innerConfigure } from '@modern-js/plugin-bff/client';
import axios from 'axios';

configure({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Modern.js uses [Hono.js](https://hono.dev/) as the BFF and Server runtime framew
Sometimes in BFF functions, it's necessary to obtain the request context to handle more logic. In such cases, you can use `useHonoContext` to get it:

```ts title="api/lambda/hello.ts"
import { useHonoContext } from '@modern-js/plugin-bff/hono'
import { useHonoContext } from '@modern-js/plugin-bff/server'
export const get = async () => {
const c = useHonoContext();
console.info(`access url: ${c.req.url}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Parameters following the dynamic path are an object called `RequestOption`, whic
In a standard function without dynamic routes, `RequestOption` can be obtained from the first parameter, for example:

```ts title="api/lambda/hello.ts"
import type { RequestOption } from '@modern-js/runtime/server';
import type { RequestOption } from '@modern-js/plugin-bff/server';

export async function post({
query,
Expand All @@ -204,7 +204,7 @@ export async function post({
Custom types can also be used here:

```ts title="api/lambda/hello.ts"
import type { RequestOption } from '@modern-js/runtime/server';
import type { RequestOption } from '@modern-js/plugin-bff/server';

type IQuery = {
// some types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The `configure` function needs to be called before all BFF requests are sent to
:::

```tsx title="routes/page.tsx"
import { configure } from '@modern-js/runtime/bff';
import { configure } from '@modern-js/plugin-bff/client';

configure({
// ...
Expand Down Expand Up @@ -90,7 +90,7 @@ There are some conventions when configuring custom request functions:
Below is an example of using axios to customize a request function:

```tsx title="App.tsx"
import { configure } from '@modern-js/runtime/bff';
import { configure } from '@modern-js/plugin-bff/client';
import type { Method, AxiosRequestHeaders as Headers } from 'axios';

configure({
Expand All @@ -105,4 +105,4 @@ configure({
return res.data;
},
});
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ Currently, two types of Hooks are available: `AfterMatch` and `AfterRender`. Dev
import type {
AfterMatchHook,
AfterRenderHook,
} from '@modern-js/runtime/server';
} from '@modern-js/server-runtime';

export const afterMatch: AfterMatchHook = (ctx, next) => {
next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Requires version x.43.0+
Create a `server/cache.[t|j]s` file in your application and export the `cacheOption` configuration to enable SSR rendering cache:

```ts title="server/cache.ts"
import type { CacheOption } from '@modern-js/runtime/server;
import type { CacheOption } from '@modern-js/server-runtime;

export const cacheOption: CacheOption = {
maxAge: 500, // ms
Expand All @@ -28,7 +28,7 @@ export const cacheOption: CacheOption = {
The caching strategy implements [stale-while-revalidate](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control).

Within the `maxAge` period, the cache content is directly returned. Exceeding `maxAge` but within `staleWhileRevalidate`, the cache content is still returned directly, but it re-renders asynchronously.

**Object Type**

```ts
Expand All @@ -52,7 +52,7 @@ export type CacheOptionProvider = (
Sometimes, developers need to use `req` to customize the cache key, or prevent caching for specific URLs. You can configure this as a function, as shown:

```ts title="server/cache.ts"
import type { CacheOption, CacheOptionProvider } from '@modern-js/runtime/server;
import type { CacheOption, CacheOptionProvider } from '@modern-js/server-runtime;

const provider: CacheOptionProvider = (req) => {
const { url, headers, ... } = req;
Expand Down Expand Up @@ -80,7 +80,7 @@ export type CacheOptions = Record<string, CacheControl | CacheOptionProvider>;
Sometimes, different routes require different caching strategies. We also offer a mapping configuration method, as shown below:

```ts title="server/cache.ts"
import type { CacheOption } from '@modern-js/runtime/server;
import type { CacheOption } from '@modern-js/server-runtime;

export const cacheOption: CacheOption = {
'/home': {
Expand Down Expand Up @@ -147,7 +147,7 @@ Developers can implement a Redis cache container as shown below:

```ts
import Redis from 'ioredis';
import type { Container, CacheOption } from '@modern-js/runtime/server;
import type { Container, CacheOption } from '@modern-js/server-runtime;

class RedisContainer implements Container {
redis = new Redis();
Expand Down Expand Up @@ -202,4 +202,4 @@ The `x-render-cache` header can have the following values:
| hit | Cache hit, returned cache content |
| stale | Cache hit, but data is stale, returned cache content and re-rendered asynchronously |
| expired | Cache expired, re-rendered and returned new content |
| miss | Cache missed |
| miss | Cache missed |
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: useHonoContext
## 使用姿势

```ts
import { useHonoContext } from '@modern-js/plugin-bff/hono';
import { useHonoContext } from '@modern-js/plugin-bff/server';
```

## 函数签名
Expand All @@ -21,7 +21,7 @@ import { useHonoContext } from '@modern-js/plugin-bff/hono';
开发者可以通过 `context` 获取更多的请求信息,例如设置响应头:

```ts
import { useHonoContext } from '@modern-js/plugin-bff/hono';
import { useHonoContext } from '@modern-js/plugin-bff/server';

export async function get() {
const c = useHonoContext();
Expand Down
Loading
Loading