The prismic nuxt tutorial/guide has a sample html serializer that includes nuxt-link. This isn't getting transformed into an actual link
See original GitHub issueIf you go over to https://prismic.io/docs/vuejs/getting-started/prismic-nuxt you’ll see that they have the following sample serializer:
import linkResolver from "./link-resolver"
import prismicDOM from 'prismic-dom'
const Elements = prismicDOM.RichText.Elements
export default function (type, element, content, children) {
// Generate links to Prismic Documents as <router-link> components
// Present by default, it is recommended to keep this
if (type === Elements.hyperlink) {
let result = ''
const url = prismicDOM.Link.url(element.data, linkResolver)
if (element.data.link_type === 'Document') {
result = `<nuxt-link to="${url}">${content}</nuxt-link>`
} else {
const target = element.data.target ? `target="'${element.data.target}'" rel="noopener"` : ''
result = `<a href="${url}" ${target}>${content}</a>`
}
return result
}
// If the image is also a link to a Prismic Document, it will return a <router-link> component
// Present by default, it is recommended to keep this
if (type === Elements.image) {
let result = `<img src="${element.url}" alt="${element.alt || ''}" copyright="${element.copyright || ''}">`
if (element.linkTo) {
const url = prismicDOM.Link.url(element.linkTo, linkResolver)
if (element.linkTo.link_type === 'Document') {
result = `<nuxt-link to="${url}">${result}</nuxt-link>`
} else {
const target = element.linkTo.target ? `target="${element.linkTo.target}" rel="noopener"` : ''
result = `<a href="${url}" ${target}>${result}</a>`
}
}
const wrapperClassList = [element.label || '', 'block-img']
result = `<p class="${wrapperClassList.join(' ')}">${result}</p>`
return result
}
// Return null to stick with the default behavior for everything else
return null
}
Which I have placed in /app/prismic/html-serializer.js
This works perfectly except <nuxt-link>
. It doesn’t get transformed/rendered. If I convert it to a standard a
tag it works fine, but obviously without handling links internally by the router. So is there a way to get this to work? I have also tried <router-link> and <prismic-link> but neither work.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:13 (4 by maintainers)
Top Results From Across the Web
The prismic nuxt tutorial/guide has a sample html serializer ...
The prismic nuxt tutorial/guide has a sample html serializer that includes nuxt-link. This isn't getting transformed into an actual link #60.
Read more >Nuxt-link on prismic website now working
The prismic nuxt tutorial/guide has a sample html serializer that includes nuxt-link. This isn't getting transformed into an actual link.
Read more >Template Content with Nuxt.js - Documentation - Prismic
The HTML Serializer defines the markup of your Rich Text. (For example: italic text should have <em> tags.) @prismicio/vue contains a default ...
Read more >How We Helped Bring You the New Nuxt 3 Link Component
So, let's discover the backstory of the Nuxt 3 link component, walk through an overview of its new features, and discuss the future...
Read more >Nuxt and Prismic - Documentation
Learn how to use Prismic with Nuxt. Prismic is a headless website builder. With a traditional CMS, you can manage a website's text...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for your input @Atinux! This thread has been very helpful for me.
Rather than using a mixin to do this at the component level, I found it more useful to wrap this solution into a clientside plugin that will handle it for all cases globally. The full solution becomes:
Implement the
data-nuxt-link
logic suggested by @Atinux into your HTML serializer.Add this file to
plugins/prismicLinks.js
nuxt.config.js
with ssr set to false:For anyone that finds this useful, I’ve put a more full example including the htmlSerializer code here: https://gist.github.com/johndigital/21b04f00abca2dca35595289fd51e680
@KristianJohansenVakwerk I placed a little hack in to make it work.
Change
<nuxt-link>
to a standard a tag with a class such as<a class="linkHandler" href="${url}">${content}</a>
Then in your code (probably in
mounted
) add an event handler on all.linkHandler
links, set them to event.preventDefault(), and manually handle the link with vue-router likethis.$router.push(event.target.pathname)
Obviously the above is a bit of pseudo code. If you can’t figure it out let me know and tomorrow or Friday I’ll copy/paste what I did.