Skip to content

Commit

Permalink
fix: prerender issue for pagination component
Browse files Browse the repository at this point in the history
Signed-off-by: nurRiyad <[email protected]>
  • Loading branch information
nurRiyad committed Aug 13, 2024
1 parent 6966c3e commit 6f7816a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion components/navbar-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
</li>
<li>
<NuxtLink
href="/news"
href="/news/pages/1"
:class="getDropdownLinkStyle('news')"
:aria-current="getAriaCurrent('news')"
@click="hideDropDownMenu"
Expand Down
12 changes: 3 additions & 9 deletions components/pagination-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
class="flex p-5 justify-center"
>
<div class="join">
<button
<NuxtLink
v-for="page in pageNumbers"
:key="page"
:class="{ 'btn-primary': page === currentPage, 'btn-disabled': page === '...' }"
:to="`/news/pages/${page}`"
class="join-item btn no-animation"
@click="handlePageChange(page)"
>
{{ page }}
</button>
</NuxtLink>
</div>
</div>
</template>
Expand Down Expand Up @@ -40,10 +40,4 @@ const pageNumbers = computed(() => {
return Array.from({ length: ttlPage.value }, (_, i) => i + 1)
}
})
const handlePageChange = (page: number | string) => {
if (typeof page === 'number') {
currentPage.value = page
}
}
</script>
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineNuxtConfig({
dataValue: 'theme', // for daisyUI
},
routeRules: {
'/news': { redirect: { to: '/news/pages/1' } },
'/products/download': { redirect: { to: '/products', statusCode: 302 } },
'/download': { redirect: { to: '/products', statusCode: 302 } },
},
Expand Down
13 changes: 13 additions & 0 deletions pages/news/index.vue → pages/news/pages/[page].vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ const { data } = await useAsyncData('news-items-list', () =>
const currentPage = ref(1)
const itemPerPage = 9
const totalBlogs = data.value?.length || 1
const totalPage = Math.ceil(totalBlogs / itemPerPage)
const route = useRoute()
watch(() => route.params.page, async (n) => {
if (typeof n === 'string') {
const pageNo = parseInt(n)
if (pageNo && pageNo > 0 && pageNo <= totalPage) {
currentPage.value = pageNo
}
else await navigateTo('/news/pages/1')
}
}, { immediate: true })
const paginatedData = computed(() => {
const startIndex = itemPerPage * (currentPage.value - 1)
Expand Down

0 comments on commit 6f7816a

Please sign in to comment.