-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: content/2.concepts/5.middleware (#48)
* wip * feat: content/2.concepts/5.middleware
- Loading branch information
Showing
10 changed files
with
164 additions
and
1 deletion.
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,3 @@ | ||
<template> | ||
<NuxtPage /> | ||
</template> |
3 changes: 3 additions & 0 deletions
3
content/2.concepts/5.middleware/.template/files/middleware/my-middleware.global.ts
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,3 @@ | ||
export default defineNuxtRouteMiddleware(() => { | ||
console.log('Hello from global middleware!') | ||
}) |
12 changes: 12 additions & 0 deletions
12
content/2.concepts/5.middleware/.template/files/pages/foo.vue
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,12 @@ | ||
<script setup lang="ts"> | ||
definePageMeta({ | ||
middleware: ['hello-foo'], | ||
}) | ||
</script> | ||
|
||
<template> | ||
<h1>Foo</h1> | ||
<NuxtLink to="/"> | ||
/Index | ||
</NuxtLink> | ||
</template> |
6 changes: 6 additions & 0 deletions
6
content/2.concepts/5.middleware/.template/files/pages/index.vue
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,6 @@ | ||
<template> | ||
<h1>Index</h1> | ||
<NuxtLink to="/foo"> | ||
/Foo | ||
</NuxtLink> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { GuideMeta } from '~/types/guides' | ||
|
||
export const meta: GuideMeta = { | ||
startingFile: 'pages/index.vue', | ||
features: { | ||
fileTree: true, | ||
}, | ||
} |
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,3 @@ | ||
<template> | ||
<NuxtPage /> | ||
</template> |
14 changes: 14 additions & 0 deletions
14
content/2.concepts/5.middleware/.template/solutions/middleware/my-middleware.global.ts
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 @@ | ||
export default defineNuxtRouteMiddleware((to) => { | ||
// FIXME: import . meta . server が置換されてしまう | ||
if (import.meta.server) | ||
return | ||
|
||
const isSignedIn = JSON.parse(localStorage.getItem('isSignedIn') || 'false') | ||
|
||
if (!isSignedIn && to.path !== '/') { | ||
return navigateTo('/') | ||
} | ||
else { | ||
return true | ||
} | ||
}) |
12 changes: 12 additions & 0 deletions
12
content/2.concepts/5.middleware/.template/solutions/pages/foo.vue
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,12 @@ | ||
<script setup lang="ts"> | ||
definePageMeta({ | ||
middleware: ['hello-foo'], | ||
}) | ||
</script> | ||
|
||
<template> | ||
<h1>Foo</h1> | ||
<NuxtLink to="/"> | ||
/Index | ||
</NuxtLink> | ||
</template> |
26 changes: 26 additions & 0 deletions
26
content/2.concepts/5.middleware/.template/solutions/pages/index.vue
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,26 @@ | ||
<script lang="ts" setup> | ||
function setIsSignedIn() { | ||
window.localStorage.setItem('isSignedIn', JSON.stringify(true)) | ||
} | ||
|
||
function removeIsSignedIn() { | ||
window.localStorage.removeItem('isSignedIn') | ||
} | ||
</script> | ||
|
||
<template> | ||
<h1>Index</h1> | ||
<NuxtLink to="/foo"> | ||
/Foo | ||
</NuxtLink> | ||
|
||
<div> | ||
<button type="button" @click="setIsSignedIn"> | ||
Set isSignedIn | ||
</button> | ||
|
||
<button type="button" @click="removeIsSignedIn"> | ||
Remove isSignedIn | ||
</button> | ||
</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