Skip to content
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

[BUGFIX] Pages en Erreur 404 sur les sites .fr et .org (PIX-1611) #216

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
}