-
Notifications
You must be signed in to change notification settings - Fork 48
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
Preview disable script #165
Conversation
- Change preview type : String to object - Activate the preview with the possibility of not loading the script
Add async and defer attributes to script
|
Name | Link |
---|---|
🔨 Latest commit | 167622d |
@lihbr , So the new pr will contain :
nuxt.options.app.head.script.push({
src: `https://static.cdn.prismic.io/prismic.min.js?repo=${repositoryName}&new=true`,
async: true,
defer: true
})
<script setup>
const enablePreview = useCookie(
'enable-preview',
// options : https://v3.nuxtjs.org/api/composables/use-cookie/
)
enablePreview.value = true
usePrismicPreview()
</script> And then add script if cookie exist add script (module.js) : const repo = options.endpoint.replace(/^https?:\/\//, '').replace(/(\.cdn)?\.prismic.+/, '')
nuxtApp.hook('page:finish', () => {
const enablePreview = useCookie(
'enable-preview'
)
if (enablePreview && options.preview) {
useHead({
script: [
{
hid: 'prismic-preview'
src: `https://static.cdn.prismic.io/prismic.js?new=true&repo=${endpoint}`,
async: true,
defer: true
}
]
})
}
}) So preview option doesn't change. With thant we have good Google Lighthouse score for performance : @lihbr , if you have any suggestions ? |
Sounds good to me 👍
Just to be clear here, by definition the toolbar script is always necessary: even outside of preview session it provides the edit button. Not loading the script is a choice, loading it only for previews is another one :)
This means the cookie would be set even when going to this page by mistake (basically it forces us to manage another "preview state" which I'm not sure is what we want). What we can do instead is check for Prismic preview cookie, like here: https://github.com/nuxt-community/prismic-module/blob/v3/src/runtime/plugin.ts#L50-L58 Note that by injecting the toolbar script only when on a preview session you also kill the "shareable preview" feature (because this type of preview relies on the script to init itself where regular previews are inited without the need of the script) I think I'd just start with loading the toolbar script with From here we can brainstorm on how to provide the toolbar script only under certain scenarios (preview), should users only want to use them in those scenarios. Let me know! |
Hi @lihbr , The module already offers to activate or deactivate the preview. So I don't think the loading strategy should come from this module. Two things :
But at the moment I don't see a solution for that.
I also try this way (using Netlify): I will look all the code for toolbar / prismic-module and keep testing different ways. |
No worries! I'm super happy you're willing to help!
Indeed, that could use some optimization. As you noticed, the toolbar is coming from far (2 years ago it's impact was >100KB / users). I don't think the current toolbar will be able to implement those optimizations though as its codebase/mechanisms are quite legacy/prone to breaking. However, we have in our backlog to overhaul it from scratch at some point and we're confident we can bring it down to a more reasonable 2/3KB for non-prismic users.
The "official" way of doing the toolbar with Prismic is quite binary as I explained above (either you add the script on every page and have the toolbar, or you don't and you don't have the toolbar) and from an official point of view we won't differ from that statement. With that in mind, this module being also a "community" module, I'm perfectly fine with us exploring alternative scenarios for loading the toolbar in some "optimized" ways while still documenting the downsides of such approaches.
That's a possibility but it's really the kind of scenario we like to avoid at Prismic because it's the kind of issue our preview system solves: you don't need a staging/preview environment, you can just preview on the live website. For code related to the toolbar:
|
Hi @lihbr ,
Yes, since its first version the toolbar has been very well optimized
🤯 really nice
You gave me a lot to think about. I think you are absolutely right.
Thanks, i'm working on a first base to get prismic, nuxt 3 and other plugins working. If I find any bugs or have any suggestions, I'll let you know. I saw that in your last commits you added async and defer for the script. And an option to disable the toolbar 🙏 |
I'm not 100% sure I got what you mean here, if you'd like to make a supercharged starter/theme for Nuxt 3 featuring Prismic I'm more than happy to help you and support you on that endeavor! It's a great initiative and if you'd like to start planning for it I guess opening a dedicated issue on this very repository could be a great place to keep track of it. On our end we don't have an official deadline yet, but Nuxt 3 support inside Slice Machine should land around the end of summer.
Yes, I also started testing the module. I'll try to finish that ASAP before publishing the module as ✨ RC ✨ |
Hi @lihbr , And nice improvement for the website 🤩 :
I'm sorry for the response delay. I'll do a full answer this weekend. |
Types of changes
Description
Activate the preview with the possibility of not loading the script
Add defer and async attributes for script
Checklist: