-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Error when using with Nuxt.js #10
Comments
Hi, I created a dummy Nuxt.js project with Capacitor and was able to reproduce this. I'm not really sure about this behaviour in Nuxt to be honest, I could only find some issues with them suggesting this solution to bypass the error. I've pushed some potential fix in the branch fix/HTMLElement_nuxtjs_10, it would be great if you can give it a try and confirm if it fixes the issue for you, though I already tested in the dummy app mentioned above and it's working fine for me. Thanks! |
How are you using/installing the branch changes? please make sure the commit changes are present there, or maybe paste them manually in the node_modules to test? When I tried this locally, it at least was working fine as long as I was not importing the |
As I said, I downloaded a ZIP of the branch with the changes (There were there!), did Maybe it’s my overly strictly configured TypeScript setup? |
I'm not really sure about the I don't think typescript is the issue here, I was able to reproduce this in a initial NuxtJs project too, but got it working after the changes. Do you have any idea why this behave this way in Nuxt, maybe cause of SSR, because of which window APIs are not available till complete rendering? To test I would suggest you to just take commit changes and paste them directly to To give you can idea what I'm testing Have also tested on iOS by the way |
I think I did everything correctly to have Nuxt use the new branch. See my terminal output from I attach the dist folder that I generated with your new branch, maybe this helps:
|
I've temporarily pushed the dummy project that I was trying to test this issue here, can you please have a look and let me know if I'm testing properly here. for package version update the package.json and copy the dist at the For me the new branch changes are working fine in this project. Regarding the issue I believe it's SSR related maybe, as browser APIs are not available on the server, see this blog for reference |
The main reason you're getting this error is because of SSR, the browser APIs like HTMLElement are not available on the server, so to use this plugin and the functionalities within in your Nuxt.js app, you need to follow these steps:
import * as SafeArea from "@aashu-dubey/capacitor-statusbar-safe-area";
export default defineNuxtPlugin(() => {
return {
provide: {
CapSafeArea: SafeArea,
},
};
});
<template>
<div>
Your Nuxt App
</div>
</template>
<script setup lang="ts">
const { $CapSafeArea } = useNuxtApp();
const insets = await $CapSafeArea?.SafeArea?.getSafeAreaInsets();
console.log("getSafeAreaInsets", insets);
// $CapSafeArea?.registerSafeAreaElement();
</script> And that's it, it should work fine after this, you don't even need the new branch I created. Check out this dummy app (will delete in a while) for a working example, and this commit for the changes required, it even is working fine with web component ( I'm not sure how you're using the plugin, but doing above steps hopefully should fix the issue for you, as I have tested it now on Web as well as on Mobile. There's an official Capacitor blog regarding using Capacitor Plugins in a Nuxt app with SSR, avoiding any SSR related errors (like in your case). |
Would really appreciate if you can try this and update if it fixes the issue for you, will also add a Nuxt.js example and details on Readme once you confirm. |
You are unbelievable, thank you for all your work. Unfortunately I‘m on vacation right now. Hope I can check your plugin solution very soon! |
Alright, no issue, enjoy your vacation 🏖️ But make sure to update once you're back 😄 |
Very good! Including your project as proper Nuxt plugin within the |
Thanks for the update, now since we know it's not an issue, I'm gonna close this. Will also update the README with this detail. |
When using within my Nuxt.js environment, I am getting an error message on
npm run dev
.When I remove "SafeAreaElement" from the plugin code, all is working well.(I do not need SafeSreaElement, only "getSafeAreaInsets"), so I could do a fork, but maybe you can have look into it.
Maybe it has something to do with TypeScript (or the lack of it?)
The text was updated successfully, but these errors were encountered: