@vercel/microfrontends 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.md +202 -0
  2. package/cli/index.cjs +21 -0
  3. package/dist/bin/cli.cjs +1604 -0
  4. package/dist/config/client.cjs +54 -0
  5. package/dist/config/client.cjs.map +1 -0
  6. package/dist/config/client.d.ts +23 -0
  7. package/dist/config/client.js +28 -0
  8. package/dist/config/client.js.map +1 -0
  9. package/dist/config/edge.cjs +508 -0
  10. package/dist/config/edge.cjs.map +1 -0
  11. package/dist/config/edge.d.ts +20 -0
  12. package/dist/config/edge.js +481 -0
  13. package/dist/config/edge.js.map +1 -0
  14. package/dist/config.cjs +1254 -0
  15. package/dist/config.cjs.map +1 -0
  16. package/dist/config.d.ts +25 -0
  17. package/dist/config.js +1212 -0
  18. package/dist/config.js.map +1 -0
  19. package/dist/index-eff254d8.d.ts +175 -0
  20. package/dist/micro-frontend-config-42886104.d.ts +154 -0
  21. package/dist/next/client.cjs +3 -0
  22. package/dist/next/client.cjs.map +1 -0
  23. package/dist/next/client.d.ts +45 -0
  24. package/dist/next/client.js +3 -0
  25. package/dist/next/client.js.map +1 -0
  26. package/dist/next/config.cjs +1793 -0
  27. package/dist/next/config.cjs.map +1 -0
  28. package/dist/next/config.d.ts +22 -0
  29. package/dist/next/config.js +1758 -0
  30. package/dist/next/config.js.map +1 -0
  31. package/dist/next/endpoints.cjs +82 -0
  32. package/dist/next/endpoints.cjs.map +1 -0
  33. package/dist/next/endpoints.d.ts +15 -0
  34. package/dist/next/endpoints.js +57 -0
  35. package/dist/next/endpoints.js.map +1 -0
  36. package/dist/next/middleware.cjs +763 -0
  37. package/dist/next/middleware.cjs.map +1 -0
  38. package/dist/next/middleware.d.ts +29 -0
  39. package/dist/next/middleware.js +735 -0
  40. package/dist/next/middleware.js.map +1 -0
  41. package/dist/next/testing.cjs +1430 -0
  42. package/dist/next/testing.cjs.map +1 -0
  43. package/dist/next/testing.d.ts +53 -0
  44. package/dist/next/testing.js +1389 -0
  45. package/dist/next/testing.js.map +1 -0
  46. package/dist/overrides.cjs +143 -0
  47. package/dist/overrides.cjs.map +1 -0
  48. package/dist/overrides.d.ts +2 -0
  49. package/dist/overrides.js +115 -0
  50. package/dist/overrides.js.map +1 -0
  51. package/dist/schema-83a75e61.d.ts +188 -0
  52. package/dist/types-4fd1c7c6.d.ts +157 -0
  53. package/dist/v2/config.cjs +720 -0
  54. package/dist/v2/config.cjs.map +1 -0
  55. package/dist/v2/config.d.ts +2 -0
  56. package/dist/v2/config.js +695 -0
  57. package/dist/v2/config.js.map +1 -0
  58. package/dist/v2/microfrontends/server.cjs +1312 -0
  59. package/dist/v2/microfrontends/server.cjs.map +1 -0
  60. package/dist/v2/microfrontends/server.d.ts +48 -0
  61. package/dist/v2/microfrontends/server.js +1277 -0
  62. package/dist/v2/microfrontends/server.js.map +1 -0
  63. package/dist/v2/microfrontends.cjs +803 -0
  64. package/dist/v2/microfrontends.cjs.map +1 -0
  65. package/dist/v2/microfrontends.d.ts +44 -0
  66. package/dist/v2/microfrontends.js +776 -0
  67. package/dist/v2/microfrontends.js.map +1 -0
  68. package/dist/v2/next/client.cjs +3 -0
  69. package/dist/v2/next/client.cjs.map +1 -0
  70. package/dist/v2/next/client.d.ts +45 -0
  71. package/dist/v2/next/client.js +3 -0
  72. package/dist/v2/next/client.js.map +1 -0
  73. package/dist/v2/next/config.cjs +1829 -0
  74. package/dist/v2/next/config.cjs.map +1 -0
  75. package/dist/v2/next/config.d.ts +22 -0
  76. package/dist/v2/next/config.js +1794 -0
  77. package/dist/v2/next/config.js.map +1 -0
  78. package/dist/v2/next/endpoints.cjs +138 -0
  79. package/dist/v2/next/endpoints.cjs.map +1 -0
  80. package/dist/v2/next/endpoints.d.ts +15 -0
  81. package/dist/v2/next/endpoints.js +113 -0
  82. package/dist/v2/next/endpoints.js.map +1 -0
  83. package/dist/v2/next/middleware.cjs +1110 -0
  84. package/dist/v2/next/middleware.cjs.map +1 -0
  85. package/dist/v2/next/middleware.d.ts +29 -0
  86. package/dist/v2/next/middleware.js +1082 -0
  87. package/dist/v2/next/middleware.js.map +1 -0
  88. package/dist/v2/schema.cjs +19 -0
  89. package/dist/v2/schema.cjs.map +1 -0
  90. package/dist/v2/schema.d.ts +1 -0
  91. package/dist/v2/schema.js +1 -0
  92. package/dist/v2/schema.js.map +1 -0
  93. package/dist/validation.cjs +754 -0
  94. package/dist/validation.cjs.map +1 -0
  95. package/dist/validation.d.ts +163 -0
  96. package/dist/validation.js +726 -0
  97. package/dist/validation.js.map +1 -0
  98. package/package.json +204 -0
  99. package/schema/schema-v2.json +288 -0
  100. package/schema/schema.json +352 -0
package/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # `@vercel/microfrontends`
2
+
3
+ Defines configuration and utilities for micro-frontend development.
4
+
5
+ ## Getting started
6
+
7
+ ### Define your `micro-frontend.json` file
8
+
9
+ The first step to get started is to define your `micro-frontend.json` file
10
+ listing all the micro-frontend applications in your setup. Since this file will
11
+ be used by all the micro-frontends, you can define it in a new NPM package.
12
+
13
+ For example, if your configuration is defined in `packages/my-app-micro-frontends-config`,
14
+ then you would define a `micro-frontends.json` at the root.
15
+
16
+ ```json filename="micro-frontends.json"
17
+ {
18
+ "version": "1",
19
+ "applications": {
20
+ "next-site": {
21
+ "default": true,
22
+ "development": {
23
+ "local": {
24
+ "protocol": "http",
25
+ "host": "localhost",
26
+ "port": 3331
27
+ },
28
+ "fallback": {
29
+ "protocol": "https",
30
+ "host": "nextjs.org"
31
+ }
32
+ },
33
+ "production": {
34
+ "protocol": "https",
35
+ "host": "nextjs.org"
36
+ },
37
+ "vercel": {
38
+ "projectId": "QmbxjYNaxv3Qphmreu8tATuLjwdhJBUxqaXC2fz3CMNwB3",
39
+ "projectName": "next-site"
40
+ }
41
+ },
42
+ "nextjs-conf": {
43
+ "default": false,
44
+ "routing": {
45
+ "assetPrefix": "nextjs-conf",
46
+ "matches": [
47
+ {
48
+ "group": "next-conf-pages",
49
+ "paths": ["/conf", "/conf/:path*"]
50
+ }
51
+ ]
52
+ },
53
+ "development": {
54
+ "fallback": {
55
+ "protocol": "https",
56
+ "host": "nextjs-conf.vercel.sh"
57
+ },
58
+ "local": {
59
+ "protocol": "http",
60
+ "host": "localhost",
61
+ "port": 3000
62
+ }
63
+ },
64
+ "production": {
65
+ "protocol": "https",
66
+ "host": "nextjs.org"
67
+ },
68
+ "vercel": {
69
+ "projectId": "prj_iv3tCRmwhF1ETZx2rH9NeoJJsoOc",
70
+ "projectName": "nextjs-conf"
71
+ }
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
77
+ In the `package.json` for the package, add the `micro-frontends.json` file to
78
+ the `files` attribute.
79
+
80
+ ```json filename="package.json"
81
+ {
82
+ "name": "my-app-micro-frontends-config",
83
+ "files": ["micro-frontends.json"]
84
+ }
85
+ ```
86
+
87
+ ### Set up Micro-Frontends in each Next.js application
88
+
89
+ #### `next.config.js`
90
+
91
+ In the `next.config.js` configuration for your application, wrap your config
92
+ with the `withMicroFrontends` function specifying the `micro-frontends.json`
93
+ config that you created above.
94
+
95
+ ```js filename="next.config.js"
96
+ const { withMicroFrontends } = require('@vercel/microfrontends/next/config');
97
+
98
+ const nextConfig = {
99
+ // ...
100
+ };
101
+
102
+ module.exports = withMicroFrontends(
103
+ nextConfig,
104
+ path.join(
105
+ __dirname,
106
+ 'node_modules/my-app-micro-frontends-config/micro-frontends.json',
107
+ ),
108
+ );
109
+ ```
110
+
111
+ This will set up the necessary environment variables and `rewrites` for the
112
+ application.
113
+
114
+ #### Middleware
115
+
116
+ Next you have to include the micro-frontends middleware. You can run this code
117
+ in your middleware to apply the micro-frontends routing logic.
118
+
119
+ ```typescript filename="middleware.ts"
120
+ import { runMicroFrontendsMiddleware } from '@vercel/microfrontends/next/middleware';
121
+
122
+ export async function middleware(
123
+ request: NextRequest,
124
+ event: NextFetchEvent,
125
+ ): Promise<Response> {
126
+ let response = await runMicroFrontendsMiddleware(request);
127
+ if (response) {
128
+ return response;
129
+ }
130
+ }
131
+ ```
132
+
133
+ #### `.well-known` API endpoints
134
+
135
+ The Micro-Frontends features rely on information exposed in two `.well-known`
136
+ endpoints:
137
+
138
+ - `/.well-known/vercel/micro-frontends/server-config`
139
+ - `/.well-known/vercel/micro-frontends/client-config`
140
+
141
+ You must define API handlers for these two routes to serve the information to
142
+ the client and Vercel Toolbar for Micro-Frontends features to work.
143
+
144
+ If you are using App Router, add a file under the `app/.well-known/vercel/micro-frontends/server-config/route.ts`
145
+ file:
146
+
147
+ ```typescript filename="app/.well-known/vercel/micro-frontends/server-config/route.ts"
148
+ export { wellKnownNextjsServerConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';
149
+ ```
150
+
151
+ Then do the same for `app/.well-known/vercel/micro-frontends/client-config/route.ts`:
152
+
153
+ ```typescript filename
154
+ export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';
155
+ ```
156
+
157
+ #### Client-side link navigation and prefetching
158
+
159
+ Lastly, to correctly navigate between micro-frontends and prefetch content, you
160
+ can use client components provided by this package.
161
+
162
+ ##### Set up prefetching in the root layout
163
+
164
+ In the root layout of your application, add the `<PrefetchCrossZoneLinks />`
165
+ element. This element will prefetch any cross-zone links to minimize the
166
+ performance impact of hard navigations.
167
+
168
+ ```typescript filename="app/layout.tsx"
169
+ export default function RootLayout({
170
+ children,
171
+ }: {
172
+ children: React.ReactNode;
173
+ }): JSX.Element {
174
+ return (
175
+ <html>
176
+ // ...
177
+ <body>
178
+ // ...
179
+ <PrefetchCrossZoneLinks />
180
+ </body>
181
+ </html>
182
+ );
183
+ }
184
+ ```
185
+
186
+ Then, use the `Link` component from `@vercel/microfrontends/next/client`
187
+ any place that you would otherwise use the `next/link` package.
188
+
189
+ ## Contributing
190
+
191
+ ### Unit Tests
192
+
193
+ ```bash
194
+ cd packages/microfrontends
195
+ pnpm unit-tests
196
+ ```
197
+
198
+ ### Integration Tests
199
+
200
+ There is an [example application](../../apps/micro-frontends-example-web) that has integration tests with Playwright.
201
+
202
+ See [README.md](../../apps/micro-frontends-example-web/README.md) for more information.
package/cli/index.cjs ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { spawnSync } = require('node:child_process');
4
+ const path = require('node:path');
5
+
6
+ const PATH_TO_DIST = path.resolve(__dirname, '../dist');
7
+
8
+ // Define the path to the CLI file
9
+ const cliPath = path.resolve(__dirname, PATH_TO_DIST, 'bin/cli.cjs');
10
+
11
+ try {
12
+ const result = spawnSync('node', [cliPath, ...process.argv.slice(2)], {
13
+ stdio: 'inherit',
14
+ });
15
+
16
+ process.exit(result.status);
17
+ } catch (error) {
18
+ // eslint-disable-next-line no-console
19
+ console.error('Error loading microfrontends CLI, please re-install', error);
20
+ process.exit(1);
21
+ }