vitral 0.11.1 → 0.11.2
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.
- package/README.md +171 -1
- package/dist/next.d.ts +1 -1
- package/dist/vue.d.ts +1 -1
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
<a href="./README.en.md">English</a>
|
|
21
21
|
</p>
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
Micro-frontends com qualquer framework, sem configurar build no host. React, Next.js, Vue, Nuxt, Angular, Svelte e outros na mesma página.
|
|
24
24
|
|
|
25
25
|
Cada equipe publica o próprio pedaço da página (um remote) com `vite build`. O host recebe a URL de cada remote e monta tudo no navegador.
|
|
26
26
|
|
|
@@ -136,6 +136,155 @@ Sem build no host, numa página HTML qualquer:
|
|
|
136
136
|
|
|
137
137
|
Todos os componentes de host aceitam `src` no lugar de `name`.
|
|
138
138
|
|
|
139
|
+
## Next.js e Nuxt como host
|
|
140
|
+
|
|
141
|
+
O remote é sempre um projeto Vite (React, Vue ou outro), como no Começo rápido acima. O host pode ser um app Next ou Nuxt.
|
|
142
|
+
|
|
143
|
+
**Next.js (App Router):**
|
|
144
|
+
|
|
145
|
+
```tsx
|
|
146
|
+
// app/carrinho.tsx
|
|
147
|
+
'use client'
|
|
148
|
+
import { configure } from 'vitral'
|
|
149
|
+
import { Remote } from 'vitral/react'
|
|
150
|
+
|
|
151
|
+
if (typeof window !== 'undefined') {
|
|
152
|
+
configure({ remotes: { carrinho: 'https://cdn.empresa.com/carrinho/vitral.json' } })
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function Carrinho({ userId }: { userId: number }) {
|
|
156
|
+
return <Remote name="carrinho" props={{ userId }} fallback={<p>Carregando…</p>} />
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```tsx
|
|
161
|
+
// app/page.tsx
|
|
162
|
+
import { Carrinho } from './carrinho'
|
|
163
|
+
|
|
164
|
+
export default function Page() {
|
|
165
|
+
return <main><Carrinho userId={42} /></main>
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**Nuxt 3 e 4:**
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
// app/plugins/vitral.client.ts
|
|
173
|
+
import { configure } from 'vitral'
|
|
174
|
+
|
|
175
|
+
export default defineNuxtPlugin(() => {
|
|
176
|
+
configure({ remotes: { catalogo: 'https://cdn.empresa.com/catalogo/vitral.json' } })
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
```vue
|
|
181
|
+
<!-- app/pages/index.vue -->
|
|
182
|
+
<script setup lang="ts">
|
|
183
|
+
import { VitralRemote } from 'vitral/vue'
|
|
184
|
+
</script>
|
|
185
|
+
|
|
186
|
+
<template>
|
|
187
|
+
<VitralRemote name="catalogo" :props="{ userId: 42 }">
|
|
188
|
+
<template #fallback><p>Carregando…</p></template>
|
|
189
|
+
</VitralRemote>
|
|
190
|
+
</template>
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Nos dois, o servidor renderiza o `fallback` e o remote monta no navegador. Não precisa de `dynamic(..., { ssr: false })` nem de `<ClientOnly>`.
|
|
194
|
+
|
|
195
|
+
## Multi-Zones ou vitral
|
|
196
|
+
|
|
197
|
+
O mesmo app Next dividido em duas áreas: a loja (app principal) e a conta, de outra equipe.
|
|
198
|
+
|
|
199
|
+
<p align="center">
|
|
200
|
+
<picture>
|
|
201
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/multizonas-escuro.png">
|
|
202
|
+
<img src="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/multizonas.png" width="900" alt="À esquerda, Multi-Zones: dois apps Next, rewrites e a página recarrega ao trocar de área (0,8 s). À direita, vitral: um app Next com withVitral, a conta é um remote dentro do layout e a troca é sem recarregar (0,2 s).">
|
|
203
|
+
</picture>
|
|
204
|
+
</p>
|
|
205
|
+
|
|
206
|
+
**Com Multi-Zones**, a conta é um segundo app Next, e o app principal repassa as URLs para ele:
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
// conta/next.config.js (a zona)
|
|
210
|
+
module.exports = {
|
|
211
|
+
basePath: '/conta',
|
|
212
|
+
assetPrefix: '/conta-static',
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
// loja/next.config.js (o app principal)
|
|
218
|
+
module.exports = {
|
|
219
|
+
async rewrites() {
|
|
220
|
+
return [
|
|
221
|
+
{ source: '/conta', destination: `${process.env.CONTA_URL}/conta` },
|
|
222
|
+
{ source: '/conta/:path+', destination: `${process.env.CONTA_URL}/conta/:path+` },
|
|
223
|
+
{ source: '/conta-static/:path+', destination: `${process.env.CONTA_URL}/conta-static/:path+` },
|
|
224
|
+
]
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
// links entre zonas: <a>, não <Link>. A página recarrega e baixa Next e React de novo.
|
|
231
|
+
<a href="/conta">Minha conta</a>
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**Com vitral**, a conta é um remote (projeto Vite) e a loja continua sendo o único app Next:
|
|
235
|
+
|
|
236
|
+
```js
|
|
237
|
+
// conta/vite.config.js (o remote)
|
|
238
|
+
import react from '@vitejs/plugin-react'
|
|
239
|
+
import vitral from 'vitral/vite'
|
|
240
|
+
|
|
241
|
+
export default { plugins: [react(), vitral({ name: 'conta', dedupe: ['react'] })] }
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
// loja/next.config.js (o host)
|
|
246
|
+
const { withVitral } = require('vitral/next-config')
|
|
247
|
+
|
|
248
|
+
module.exports = withVitral({}, {
|
|
249
|
+
remotes: {
|
|
250
|
+
conta: { url: 'https://cdn.empresa.com/conta/vitral.json', basePath: '/conta' },
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
// loja/pages/_app.tsx
|
|
257
|
+
import { VitralNext } from 'vitral/next-host'
|
|
258
|
+
|
|
259
|
+
export default function App({ Component, pageProps }) {
|
|
260
|
+
return <><VitralNext /><Component {...pageProps} /></>
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
```tsx
|
|
265
|
+
// loja/pages/conta/[[...slug]].tsx
|
|
266
|
+
import { NextRemote } from 'vitral/next-host'
|
|
267
|
+
|
|
268
|
+
export default function Conta() {
|
|
269
|
+
return <Layout><NextRemote name="conta" fallback={<p>Carregando…</p>} /></Layout>
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
```tsx
|
|
274
|
+
// links: <Link> normal. A troca é sem recarregar, e o React não baixa de novo.
|
|
275
|
+
<Link href="/conta">Minha conta</Link>
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
| | Multi-Zones | vitral |
|
|
279
|
+
|---|---|---|
|
|
280
|
+
| Apps Next | Um por área | Só o host |
|
|
281
|
+
| Configuração | `basePath` e `assetPrefix` na zona, `rewrites` no app principal | `withVitral` no host, plugin no remote |
|
|
282
|
+
| Trocar de área | Recarrega a página (0,8 s no benchmark) | Sem recarregar (0,2 s) |
|
|
283
|
+
| React e Next | Baixados de novo em cada área | Uma cópia para a página toda |
|
|
284
|
+
| Layout do app principal | Cada zona repete o seu | O remote fica dentro do layout do host |
|
|
285
|
+
|
|
286
|
+
Guia completo, com SSR por rota e prefetch: [Migrar do Next.js Multi-Zones](https://joaogabriel15.github.io/vitral/migracao/multi-zones).
|
|
287
|
+
|
|
139
288
|
## Rodar local
|
|
140
289
|
|
|
141
290
|
Cada remote roda no próprio `vite dev`, numa porta. O host aponta para essas portas:
|
|
@@ -262,6 +411,13 @@ O que entra em `${}` é escapado. Mudar `state` ou as props atualiza a tela.
|
|
|
262
411
|
|
|
263
412
|
## Comunicação entre remotes
|
|
264
413
|
|
|
414
|
+
<p align="center">
|
|
415
|
+
<picture>
|
|
416
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/comunicacao-escuro.png">
|
|
417
|
+
<img src="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/comunicacao.png" width="900" alt="Três remotes em React, Vue e Angular. O carrinho emite um evento, a busca recebe pelo mesmo barramento, e a conta lê o usuário que o host criou com provide.">
|
|
418
|
+
</picture>
|
|
419
|
+
</p>
|
|
420
|
+
|
|
265
421
|
```js
|
|
266
422
|
import { events } from 'vitral'
|
|
267
423
|
|
|
@@ -415,6 +571,13 @@ Um remote local (`module`) só mede `css` e `mount`. Erros dentro do
|
|
|
415
571
|
|
|
416
572
|
Para não baixar React ou Vue mais de uma vez, liste-os em `dedupe` no remote:
|
|
417
573
|
|
|
574
|
+
<p align="center">
|
|
575
|
+
<picture>
|
|
576
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/dedupe-escuro.png">
|
|
577
|
+
<img src="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/dedupe.png" width="900" alt="Sem dedupe, cada remote baixa o próprio React. Com dedupe, os remotes usam o React que já está na página quando a versão serve; o que não serve baixa o próprio.">
|
|
578
|
+
</picture>
|
|
579
|
+
</p>
|
|
580
|
+
|
|
418
581
|
```js
|
|
419
582
|
vitral({ dedupe: ['react'] })
|
|
420
583
|
```
|
|
@@ -431,6 +594,13 @@ página (`watchUpdates()` e `refresh('carrinho')`) e app shell por rota
|
|
|
431
594
|
|
|
432
595
|
## Segurança
|
|
433
596
|
|
|
597
|
+
<p align="center">
|
|
598
|
+
<picture>
|
|
599
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/seguranca-escuro.png">
|
|
600
|
+
<img src="https://raw.githubusercontent.com/joaogabriel15/vitral/main/docs/public/marca/seguranca.png" width="900" alt="O host configura allowedOrigins e integrity. Antes de rodar, a lib confere a origem e o SHA-384. Se um dos dois falha, nada executa e o erro aparece no lugar do remote.">
|
|
601
|
+
</picture>
|
|
602
|
+
</p>
|
|
603
|
+
|
|
434
604
|
- Só importa JS de origens permitidas, mesmo que o `vitral.json` aponte para outra. O `src` de um componente de host entra na allowlist; o atributo `src` do `<vitral-remote>` não entra quando há `allowedOrigins`.
|
|
435
605
|
- Com `integrity`, nada roda sem o hash conferir. A lib usa o primeiro caminho que o navegador garante: import map com `integrity` (Chrome 133+, Safari 18.4+), `<link rel="modulepreload" integrity>` (Firefox 115+) ou, nos outros, baixa, confere e executa exatamente os bytes conferidos via `blob:`. Aceita `sha256`, `sha384` e `sha512`, e vários hashes separados por espaço (vale o mais forte). O CSS do manifesto também é conferido.
|
|
436
606
|
- Com CSP, a lib copia o `nonce` de um `<script nonce>` da página para o import map. Sem nonce, cai para o `modulepreload`.
|
package/dist/next.d.ts
CHANGED
|
@@ -118,7 +118,7 @@ type ImageProps = Omit<ImgHTMLAttributes<HTMLImageElement>, 'src' | 'width' | 'h
|
|
|
118
118
|
}) => string;
|
|
119
119
|
onLoadingComplete?: (img: HTMLImageElement) => void;
|
|
120
120
|
};
|
|
121
|
-
declare const Image: react.ForwardRefExoticComponent<Omit<ImgHTMLAttributes<HTMLImageElement>, "
|
|
121
|
+
declare const Image: react.ForwardRefExoticComponent<Omit<ImgHTMLAttributes<HTMLImageElement>, "height" | "width" | "loading" | "src"> & {
|
|
122
122
|
src: string | StaticImage;
|
|
123
123
|
alt: string;
|
|
124
124
|
width?: number | string;
|
package/dist/vue.d.ts
CHANGED
|
@@ -127,9 +127,9 @@ declare const VitralRemote: vue.DefineComponent<vue.ExtractPropTypes<{
|
|
|
127
127
|
onError?: ((...args: any[]) => any) | undefined;
|
|
128
128
|
onReady?: ((...args: any[]) => any) | undefined;
|
|
129
129
|
}>, {
|
|
130
|
+
name: string;
|
|
130
131
|
isolate: Isolation;
|
|
131
132
|
priority: RequestPriority;
|
|
132
|
-
name: string;
|
|
133
133
|
loading: Loading;
|
|
134
134
|
src: string;
|
|
135
135
|
props: Props;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitral",
|
|
3
|
-
"version": "0.11.
|
|
4
|
-
"description": "Micro-frontends
|
|
3
|
+
"version": "0.11.2",
|
|
4
|
+
"description": "Micro-frontends com qualquer framework, sem configurar build no host. React, Next.js, Vue, Nuxt, Angular, Svelte e outros na mesma página.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"micro-frontend",
|
|
7
7
|
"micro-frontends",
|
|
@@ -27,7 +27,11 @@
|
|
|
27
27
|
"astro",
|
|
28
28
|
"lit",
|
|
29
29
|
"web-components",
|
|
30
|
-
"typescript"
|
|
30
|
+
"typescript",
|
|
31
|
+
"micro-frontend-framework",
|
|
32
|
+
"module-federation-alternative",
|
|
33
|
+
"nextjs-multi-zones",
|
|
34
|
+
"multi-zones"
|
|
31
35
|
],
|
|
32
36
|
"homepage": "https://joaogabriel15.github.io/vitral/",
|
|
33
37
|
"bugs": {
|