Skip to content

Commit

Permalink
feat(api): avoid API calling for path with hash (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mercs600 committed Feb 6, 2023
1 parent 8140572 commit 8a1f44c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/runtime/composables/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Component } from 'vue'
import { RouteLocationNormalized } from 'vue-router'
import { pascalCase } from 'scule'
import { cleanDoubleSlashes } from 'ufo'
import { cleanDoubleSlashes, isEqual } from 'ufo'
import { NuxtApp } from 'nuxt/dist/app/nuxt'
import defu from 'defu'
import type { FetchOptions } from 'ohmyfetch'
Expand All @@ -28,10 +28,20 @@ export const useT3Redirect = (redirectData: T3RedirectData) => {
* @param {RouteLocationNormalized} route current route
* @returns {Boolean}
*/
export const isDynamicRoute = (route: RouteLocationNormalized) => {
export const isDynamicRoute = (route: RouteLocationNormalized): boolean => {
return route?.matched?.[0]?.path.includes('/:slug(.*)*')
}

/**
* Check if provieded paths are equal after remove hashes
* @param {String} a
* @param {String} b
* @returns {Boolean}
*/
export const isEqualWithoutHash = (a: string, b: string): boolean => {
return isEqual(a?.split('#')?.[0], b.split('#')?.[0])
}

/**
* Get component name for T3Renderer
* @param {String} type Content Element type
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/middleware/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isEqual } from 'ufo'
import type { RouteLocationNormalized } from 'vue-router'
import { callWithNuxt, useNuxtApp } from '#app'
import { useT3Api } from '../composables/useT3Api'
Expand All @@ -9,7 +8,8 @@ import {
import {
useT3Redirect,
isDynamicRoute,
useT3LocalePath
useT3LocalePath,
isEqualWithoutHash
} from '../composables/utils'

export async function t3ContextMiddleware (
Expand All @@ -32,7 +32,7 @@ export async function t3ContextMiddleware (
)
}
}
if (dynamicRoute && (process.server || !isEqual(to.fullPath, from.fullPath))) {
if (dynamicRoute && (process.server || !isEqualWithoutHash(to.fullPath, from.fullPath))) {
nuxtApp.callHook('page:start')
try {
const data = await getPage(to.fullPath)
Expand Down

0 comments on commit 8a1f44c

Please sign in to comment.