Skip to content

Commit 5d9740a

Browse files
authored
Merge pull request #242 from fabricioOak/documentation-external-navigation
2 parents 14957b7 + 958b8cb commit 5d9740a

File tree

1 file changed

+69
-0
lines changed
  • docs/content/1.documentation/5.advanced

1 file changed

+69
-0
lines changed

docs/content/1.documentation/5.advanced/2.faq.md

+69
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,72 @@ security:{
145145
::alert{type="info"}
146146
ℹ Read more about it [here](https://github.com/Baroshem/nuxt-security/issues/228).
147147
::
148+
149+
## Updating Headers on an especific route
150+
151+
Sometimes you may want to override the default headers on an especific route to allow a certain script to be loaded. You can do that by using the `routeRules` option and loading the headers again by setting the navigation to that route to act as `external`:
152+
153+
```ts
154+
routeRules: {
155+
// The default headers for all routes
156+
'/**': {
157+
headers: {
158+
'Cross-Origin-Embedder-Policy': 'require-corp'
159+
}
160+
},
161+
// The headers for the route you want to override
162+
'/example': {
163+
headers: {
164+
'Cross-Origin-Embedder-Policy': 'unsafe-none'
165+
}
166+
},
167+
},
168+
```
169+
170+
### Using a Middleware
171+
172+
You can create a middleware to handle external navigation as follows:
173+
174+
```ts
175+
// middleware/external-navigation.ts
176+
177+
export default defineNuxtRouteMiddleware((to) => {
178+
const routesForExternalLinks = ['/example']
179+
// Add any other routes you want to act as external links
180+
181+
if (
182+
process.client &&
183+
!useNuxtApp().isHydrating &&
184+
to.path &&
185+
routesForExternalLinks.includes(to.path)
186+
) {
187+
window.location.href = to.fullPath
188+
}
189+
})
190+
191+
```
192+
193+
To use this middleware, include it in your script:
194+
195+
```ts
196+
// example.vue
197+
198+
<script lang="ts" setup>
199+
definePageMeta({
200+
middleware: ['external-navigation']
201+
})
202+
</script>
203+
```
204+
### Using NuxtLink
205+
206+
Alternatively, you can use the `external` attribute on `NuxtLink` to set the navigation to external:
207+
208+
```html
209+
<NuxtLink :to="{name : 'example'}" :external="true">
210+
Example
211+
</NuxtLink>
212+
```
213+
214+
::alert{type="info"}
215+
ℹ Read more about it [here](https://github.com/Baroshem/nuxt-security/issues/228).
216+
::

0 commit comments

Comments
 (0)