Finally fix the build pagination issue
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
import type { LoadInput, LoadOutput } from '@sveltejs/kit/types/page'
|
||||
|
||||
export async function load({ fetch, url }: LoadInput): Promise<LoadOutput> {
|
||||
const blogPostsResponse = await fetch(`/blog/articles?limit=5`)
|
||||
const blogPostsResponse = await fetch(`/blog/articles/pageSize/5.json`)
|
||||
const blogPostsContent = await blogPostsResponse.json()
|
||||
return {
|
||||
props: {
|
||||
|
@ -1,21 +1,13 @@
|
||||
<script lang="ts" context="module">
|
||||
import {
|
||||
getPaginationSearchParams,
|
||||
parseParams,
|
||||
} from '$lib/pagination/searchParams'
|
||||
|
||||
const pageSize = 7
|
||||
import { parseParams } from '$lib/pagination/dropTakeParams'
|
||||
|
||||
/**
|
||||
* @type {import('@sveltejs/kit').Load}
|
||||
*/
|
||||
export async function load({ fetch, params }) {
|
||||
console.log('params', params)
|
||||
const { page = 1, ...filters } = parseParams(params.params)
|
||||
const searchParams = getPaginationSearchParams({ pageSize, page, filters })
|
||||
console.log('searchpprsm', searchParams)
|
||||
const { page = 1, pageSize = 7, ...filters } = parseParams(params.params)
|
||||
const articleResponse = await fetch(
|
||||
`/blog/articles?${searchParams.toString()}`
|
||||
`/blog/articles/${params.params ? params.params : 'index'}.json`
|
||||
).then((r) => r.json())
|
||||
|
||||
return {
|
||||
@ -40,6 +32,7 @@
|
||||
export let filters: Record<string, string>
|
||||
export let page: number
|
||||
export let pageSize: number
|
||||
|
||||
let totalPages = Math.ceil(posts.totalCount / pageSize)
|
||||
</script>
|
||||
|
||||
@ -47,7 +40,6 @@
|
||||
<title>My blog @michalvankodev</title>
|
||||
</svelte:head>
|
||||
|
||||
{@debug posts}
|
||||
{#if posts.items.length === 0}
|
||||
<p class="no-posts">You've found void in the space.</p>
|
||||
{:else}
|
||||
@ -75,7 +67,7 @@
|
||||
/>
|
||||
</header>
|
||||
<ul class="post-list {postListClass}">
|
||||
{#each posts.items as post}
|
||||
{#each posts.items as post (post.slug)}
|
||||
<li>
|
||||
<article>
|
||||
<header>
|
||||
|
@ -1,14 +0,0 @@
|
||||
import { getPaginationQueryFromSearchParams } from '$lib/pagination/searchParams'
|
||||
import { getBlogListing } from './_content'
|
||||
|
||||
export async function get({ url: { searchParams } }) {
|
||||
const paginationQuery = getPaginationQueryFromSearchParams(searchParams)
|
||||
const filteredContents = await getBlogListing(paginationQuery)
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
posts: filteredContents,
|
||||
},
|
||||
}
|
||||
}
|
25
src/routes/blog/articles/[...params].json.ts
Normal file
25
src/routes/blog/articles/[...params].json.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import {
|
||||
getDropTakeFromPageParams,
|
||||
parseParams,
|
||||
} from '$lib/pagination/dropTakeParams'
|
||||
import { getBlogListing } from '../_content'
|
||||
|
||||
export async function get({ params }) {
|
||||
console.log('article-params', params)
|
||||
const handledParams = params.params === 'index' ? '' : params.params
|
||||
const { page = 1, pageSize = 7, ...filters } = parseParams(handledParams)
|
||||
const paginationParams = getDropTakeFromPageParams(
|
||||
Number(pageSize),
|
||||
Number(page)
|
||||
)
|
||||
const paginationQuery = { ...paginationParams, filters }
|
||||
const filteredContents = await getBlogListing(paginationQuery)
|
||||
console.log(filteredContents.items.map((item) => item.slug))
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
posts: filteredContents,
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user