New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to rename window.__NUXT__ context object #18870
Comments
As a workaround for my experimentation, I'm temporarily resolving this by extending the Vite and Nitro configs in import replace from '@rollup/plugin-replace';
const replacePlugin = replace({
__NUXT__: '__NUXT_EXAMPLE__',
});
const isDevelopmentEnvironment = process.env.NODE_ENV === 'development';
export default defineNuxtConfig({
vite: {
build: {
rollupOptions: {
plugins: isDevelopmentEnvironment ? [] : [renameNuxtContextPlugin],
},
},
},
nitro: {
rollupConfig: {
plugins: isDevelopmentEnvironment ? [] : [renameNuxtContextPlugin],
},
},
}); |
The |
I looked at the description and it seems to me that there is no need for additional work here. If I am mistaken, please let me know. I could take a look at this issue :) |
It's worth noting that |
We're not inclined to support renaming |
HI @mvanalphen , even if this issue is closed, did you encountered more issues? |
Hey @victorgarciaesgi! For us, the solution of renaming However, as our second Nuxt application was dynamically loaded into the first Nuxt application based on an action by the user, we had to ensure the Nuxt window object was automatically injected onto the page. As by default the Nuxt window object is added as an inline script to the <script>window.__NUXT_EXAMPLE__=(function(a){return {serverRendered:a,config:{public:{isDevelopmentEnvironment:a},app:{baseURL:"\u002F",buildAssetsDir:"\u002F_nuxt\u002F",cdnURL:""}},data:{},state:{}}}(false))</script> But of course, we can't rely on an // nitro-plugins/inject-context.ts
import { readFileSync, writeFileSync } from 'fs';
export default defineNitroPlugin(nitroApp => {
nitroApp.hooks.hook('render:html', (html, { event }) => {
if (!event.context.params?._.includes('index.html')) {
return;
}
const nuxtContextScript = html.bodyAppend.find(
(element: string) => element.includes('<script>') && element.includes(`window.__NUXT_EXAMPLE__`)
);
if (!nuxtContextScript) {
return;
}
const strippedContext = `${nuxtContextScript
.replace('<script>', '')
.replace('</script>', '')};`;
const entryFilePath = './.output/public/_nuxt/entry.js';
const entryFileData = readFileSync(entryFilePath);
writeFileSync(entryFilePath, `${strippedContext}${entryFileData}`);
});
}); So At that point, it's just a matter of creating the element on the page ( Definitely not the most elegant or perfect solution, but it works well for our use case and performance seems to be fine. |
Thanks for the detailed response @mvanalphen ! Super cool solution we'll try that |
Happy to help! Hopefully it'll work for you as well! And here's to hoping that the upcoming multi-app solution will make this a breeze 😄. |
Sorry to bother you again @mvanalphen , but did you by chance encountered a The rollup replace plugin worked, and I have the 2nd app loaded in the first one dynamically (user action) But it seems when mounting Nuxt still have 2 instances cohabiting, is it something your nitro plugin prevented? BTW: I'm in dev mode with a different build for MF, enabled the rollup plugin in Vite dev too. I saw the |
@victorgarciaesgi No worries! I can't say that's an error I've ever run into. Am I understanding correctly that you're only running into this error in development mode ( If you're only running into this issue in development mode, I'm guessing that's because the Nitro plugin only works during |
I managed to made it working in dev without the nitro plugin. const replacePlugin: any = replace({
delimiters: ['', ''],
values: {
__NUXT__: '__NUXT_MF__',
[`getContext("nuxt-app")`]: `getContext("nuxt-app-mf")`,
},
}); Had to remove the routeRules: {
'/mf/project-client': {redirect: '/'},
} Still a lot of hacky stuff, working on dev, not tested on prod build yet Thanks again for your help and infos @mvanalphen :D |
No worries, and glad to hear you found a solution and thanks for sharing it here. Small steps, but closely getting there 😄. |
Describe the feature
In Nuxt 2, it was possible to rename the
__NUXT__
context object assigned to thewindow
through theglobals.context
option innuxt.config.js
, as documented here.In Nuxt 3, it seems that it is not possible to rename this context object through the Nuxt configuration. However, in this pull request, the possibility of changing the
rootId
androotTag
was re-added, which was previously possible through the aforementionedglobals
configuration option.As such, the feature request is to allow clients to rename the
__NUXT__
context object through their Nuxt configuration.Context
For context: I'm currently experimenting with loading a new Nuxt application within an existing Nuxt application. The reason for this is that we run a number of micro-frontends as separate Nuxt projects. When a user clicks on an element that exists in some of these Nuxt micro-frontends, we want to show a slide-in component which is actually a separately maintained Nuxt or Vue project. We're still running some experiments in how we want to handle this, with one option being loading an entirely new Nuxt application.
An issue with loading a new Nuxt application, is that the micro-frontends naturally define their own
__NUXT__
context, while the dynamically loaded project will also want to do the same. These will conflict with each other. As such, the dynamically loaded project would want to use its ownwindow.__NUXT__
context object, e.g.__NUXT_EXAMPLE__
.This is all still in very early stages and it may be a pretty bad idea, and I may end up just building this project in vanilla Vue 3. However, I figured that it may be worthwhile to re-add this functionality from Nuxt 2.
Additional information
Final checks
The text was updated successfully, but these errors were encountered: