-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from KyleSmith0905/use-nuxt-route-rules
Added route-based rules for cache-support.
- Loading branch information
Showing
21 changed files
with
343 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
description: "Learn how to configure your project to support caching" | ||
--- | ||
|
||
::alert{type="info"} | ||
If you are using the following routeRules (`swr`, `isr`, `prerender`), you will need to read this. When prerendering your entire site using `nuxi generate`, this is done automatically. | ||
:: | ||
|
||
# Caching Content | ||
|
||
Often hosting providers offer caching on the edge. Most websites can experience incredible speeds (and cost savings) by taking advantage of caching. No cold start, no processing requests, no parsing Javascript... just HTML served immediately from a CDN. | ||
|
||
By default we send the user's authentication data down to the client in the HTML. This might not be ideal if you're caching your pages. Users may be able to see other user's authentication data if not handled properly. | ||
|
||
To add caching to your Nuxt app, follow the [Nuxt documentation on hybrid rendering](https://nuxt.com/docs/guide/concepts/rendering#hybrid-rendering). | ||
|
||
## Configuration | ||
|
||
::alert{type="warning"} | ||
If you find yourself needing to server-rendered auth methods like `getProviders()`, you must set the `baseURL` option on the `auth` object. This applies in development too. | ||
:: | ||
|
||
### Page Specific Cache Rules | ||
|
||
If only a few of your pages are cached. Head over to the Nuxt config `routeRules`, add the `auth` key to your cached routes. Set `disableServerSideAuth` to `true`. | ||
|
||
```ts | ||
export default defineNuxtConfig({ | ||
modules: ['@sidebase/nuxt-auth'], | ||
auth: { | ||
// Optional - Needed for getProviders() method to work server-side | ||
baseURL: 'http://localhost:3000', | ||
}, | ||
routeRules: { | ||
'/': { | ||
swr: 86400000, | ||
auth: { | ||
disableServerSideAuth: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
``` | ||
|
||
### Module Cache Rules | ||
|
||
If all/most pages on your site are cached. Head over to the Nuxt config, add the `auth` key if not already there. Set `disableServerSideAuth` to `true`. | ||
|
||
```ts | ||
export default defineNuxtConfig({ | ||
modules: ['@sidebase/nuxt-auth'], | ||
auth: { | ||
disableServerSideAuth: true, | ||
// Optional - Needed for getProviders() method to work server-side | ||
baseURL: 'http://localhost:3000', | ||
}, | ||
}) | ||
``` | ||
|
||
### Combining Configurations | ||
|
||
Route-configured options take precedent over module-configured options. If you disabled server side auth in the module, you may still enable server side auth back by setting `auth.disableServerSideAuth` to `false`. | ||
|
||
For example: It may be ideal to add caching to every page besides your profile page. | ||
|
||
```ts | ||
export default defineNuxtConfig({ | ||
modules: ['@sidebase/nuxt-auth'], | ||
auth: { | ||
disableServerSideAuth: true, | ||
}, | ||
routeRules: { | ||
// Server side auth is disabled on this page | ||
'/': { | ||
swr: 86400000, | ||
} | ||
// Server side auth is enabled on this page - route rules takes priority. | ||
'/profile': { | ||
auth: { | ||
disableServerSideAuth: false, | ||
}, | ||
}, | ||
}, | ||
}) | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Route Rules | ||
|
||
Use the `auth`-key inside the `nuxt.config.ts` `routeRules` to configure page-specific settings. | ||
|
||
```ts | ||
interface RouteOptions { | ||
/** | ||
* Forces your server to send a "loading" status on a route, prompting the client to fetch on the client. If a specific page has caching, this prevents the server from caching someone's authentication status. | ||
* | ||
* @default falses | ||
*/ | ||
disableServerSideAuth: boolean; | ||
} | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<script setup lang="ts"> | ||
import { computed, definePageMeta, onMounted, ref } from '#imports' | ||
const cachedAt = ref(new Date()) | ||
const enteredAt = ref<Date | undefined>(undefined) | ||
const cachedAtTime = computed(() => cachedAt.value.getTime()) | ||
const enteredAtTime = computed(() => (enteredAt.value?.getTime() ?? 0)) | ||
const relativeTimeFormat = ref(new Intl.RelativeTimeFormat('en')) | ||
onMounted(() => { | ||
enteredAt.value = new Date() | ||
}) | ||
definePageMeta({ auth: false }) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p v-if="cachedAtTime < enteredAtTime - 5000"> | ||
This page was cached | ||
{{ relativeTimeFormat.format((cachedAtTime / 60000) - (enteredAtTime / 60000), 'minutes') }}. | ||
</p> | ||
<p v-else> | ||
This page was not cached. | ||
</p> | ||
<p>Cached At: {{ enteredAt?.toISOString() }}.</p> | ||
<p>Created At: {{ enteredAt?.toISOString() }}.</p> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<script setup lang="ts"> | ||
import { computed, definePageMeta, onMounted, ref } from '#imports' | ||
const cachedAt = ref(new Date()) | ||
const enteredAt = ref<Date | undefined>(undefined) | ||
const cachedAtTime = computed(() => cachedAt.value.getTime()) | ||
const enteredAtTime = computed(() => (enteredAt.value?.getTime() ?? 0)) | ||
const relativeTimeFormat = ref(new Intl.RelativeTimeFormat('en')) | ||
onMounted(() => { | ||
enteredAt.value = new Date() | ||
}) | ||
definePageMeta({ auth: false }) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p v-if="cachedAtTime < enteredAtTime - 5000"> | ||
This page was cached | ||
{{ relativeTimeFormat.format((cachedAtTime / 60000) - (enteredAtTime / 60000), 'minutes') }}. | ||
</p> | ||
<p v-else> | ||
This page was not cached. | ||
</p> | ||
<p>Cached At: {{ enteredAt?.toISOString() }}.</p> | ||
<p>Created At: {{ enteredAt?.toISOString() }}.</p> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<script setup lang="ts"> | ||
import { computed, definePageMeta, onMounted, ref } from '#imports' | ||
const cachedAt = ref(new Date()) | ||
const enteredAt = ref<Date | undefined>(undefined) | ||
const cachedAtTime = computed(() => cachedAt.value.getTime()) | ||
const enteredAtTime = computed(() => (enteredAt.value?.getTime() ?? 0)) | ||
const relativeTimeFormat = ref(new Intl.RelativeTimeFormat('en')) | ||
onMounted(() => { | ||
enteredAt.value = new Date() | ||
}) | ||
definePageMeta({ auth: false }) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p v-if="cachedAtTime < enteredAtTime - 5000"> | ||
This page was cached | ||
{{ relativeTimeFormat.format((cachedAtTime / 60000) - (enteredAtTime / 60000), 'minutes') }}. | ||
</p> | ||
<p v-else> | ||
This page was not cached. | ||
</p> | ||
<p>Cached At: {{ enteredAt?.toISOString() }}.</p> | ||
<p>Created At: {{ enteredAt?.toISOString() }}.</p> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.