Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(bridge): add useNuxtApp and defineNuxtPlugin composables #576

Merged
merged 6 commits into from
Sep 25, 2021

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Sep 24, 2021

πŸ”— Linked issue

nuxt/bridge#105

❓ Type of change

  • New feature (a non-breaking change that adds functionality)

πŸ“š Description

This adds a shim for useNuxtApp intended to be compatible in API:

export function createNuxt (options: CreateOptions) {
const nuxt: NuxtApp = {
provide: undefined,
globalName: 'nuxt',
state: {},
payload: {},
isHydrating: process.client,
...options
} as any as NuxtApp
nuxt.hooks = createHooks<RuntimeNuxtHooks>()
nuxt.hook = nuxt.hooks.hook
nuxt.callHook = nuxt.hooks.callHook
.

I have noted a question below about nuxt.state as this is currently unused in nuxt3.

I can follow-up with a second PR to add defineNuxtPlugin which will work quite closely with this composable, or can implement it here too.

Finally, perhaps we can update docs after nuxt/bridge#102 is complete. (Or I can document this individually if preferred.)

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@danielroe danielroe added enhancement New feature or request bridge labels Sep 24, 2021
@danielroe danielroe self-assigned this Sep 24, 2021
@danielroe danielroe requested a review from pi0 September 24, 2021 12:35
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Sep 24, 2021

Deploying with Β Cloudflare Pages Β Cloudflare Pages

Latest commit: a599b84
Status:Β βœ…Β  Deploy successful!
Preview URL: https://4e50da91.framework-1rm.pages.dev

View logs

const vm = getCurrentInstance()

if (!vm) {
throw new Error('nuxt instance unavailable')
Copy link
Member

@pi0 pi0 Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check nuxt 3 implementation: https://github.com/nuxt/framework/blob/main/packages/nuxt3/src/app/nuxt.ts#L181

We should be able to use nuxt app instance outside of vue lifecycle in the client-side (replacing global $nuxt usage) and during the execution of nuxt plugins (making composables for plugins)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on its way!

packages/bridge/src/runtime/app.plugin.mjs Outdated Show resolved Hide resolved
packages/bridge/src/runtime/app.plugin.mjs Show resolved Hide resolved
}

ctx.app.created.push(function () {
nuxt.app = this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inconsistent with n3 and finally app instances are not the same. Maybe keeping ctx.app and this (created app) as different vue2 only names in the nuxt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: We would need to expose NuxtAppCompat interface ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. So something like:

{
  legacyApp, // => ctx.app (app options)
  legacyThis, // => this
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call first legacyNuxt (it is nuxt interface right?) and legacyApp (vue2 app instance?)

@danielroe danielroe changed the title feat(bridge): add useNuxtApp composable feat(bridge): add useNuxtApp and defineNuxtPlugin composables Sep 24, 2021
@pi0 pi0 merged commit 3bf8568 into main Sep 25, 2021
@pi0 pi0 deleted the feat/bridge-use-nuxt-app branch September 25, 2021 08:24
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3.x bridge enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants