Skip to content

Commit

Permalink
[BUGFIX] Pages en Erreur 404 sur les sites .fr et .org (PIX-1611)
Browse files Browse the repository at this point in the history
Merge pull request #216 from 1024pix/fix-links-in-rich-texts
  • Loading branch information
bpetetot authored Nov 16, 2020
2 parents 5667109 + 2d3aac4 commit 4ac0fc2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
18 changes: 16 additions & 2 deletions components/PixLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ export default {
const relativeLinkPrefix = getRelativeLinkPrefix(url)
if (this.field.link_type === 'Document') {
const localeURL = getLocaleUrl(url, this.localePath)
template = `
<router-link to="${this.localePath(url)}" exact>
<router-link to="${localeURL}" exact>
<slot/>
</router-link>
`
} else if (relativeLinkPrefix) {
const relativeUrl = url.replace(relativeLinkPrefix, '/')
const localeURL = getLocaleUrl(relativeUrl, this.localePath)
template = `
<router-link to="${this.localePath(relativeUrl)}" exact>
<router-link to="${localeURL}" exact>
<slot/>
</router-link>
`
Expand All @@ -51,6 +53,18 @@ export default {
},
},
}
function getLocaleUrl(url, localePath) {
if (
url.startsWith('/fr') ||
url.startsWith('/en-gb') ||
url.startsWith('/fr-fr')
) {
return url
}
return localePath(url)
}
function getRelativeLinkPrefix(url) {
if (!url) {
return ''
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default {
'~/plugins/meta.js',
{ src: '~plugins/slide-menu', ssr: false },
'~plugins/vue-js-modal',
{ src: '~/plugins/prismicLinks', ssr: false },
],

/*
Expand Down
2 changes: 1 addition & 1 deletion plugins/html-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function (type, element, content, children) {
const url = prismicDOM.Link.url(element.data, linkResolver)

if (element.data.link_type === 'Document') {
result = `<nuxt-link to="${url}">${content}</nuxt-link>`
result = `<a href="${url}" data-nuxt-link>${content}</a>`
} else {
const target = element.data.target
? `target="'${element.data.target}'" rel="noopener"`
Expand Down
4 changes: 3 additions & 1 deletion plugins/link-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export default function (doc) {
'cgu_page',
'statistiques',
'legal-notices',
'simple_page',
]
if (staticRoute.includes(doc.type)) {
return `/${doc.uid}`
const locale = doc.lang !== 'fr-fr' ? `/${doc.lang}` : ''
return `${locale}/${doc.uid}`
}
if (doc.type === 'news_item') {
return `/actualites/${doc.uid}`
Expand Down
16 changes: 16 additions & 0 deletions plugins/prismicLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default ({ redirect }) => {
window.addEventListener(
'click',
(event) => {
// If the clicked element doesn't have the right selector, bail
if (!event.target.matches('a[data-nuxt-link]')) return

// Don't follow the link
event.preventDefault()

// Push link destination to router
redirect(event.target.pathname)
},
false
)
}

0 comments on commit 4ac0fc2

Please sign in to comment.