Skip to content

Commit

Permalink
Merge pull request #114 from ssss-sfu/vhn/nextjs-fix-sanity-blog
Browse files Browse the repository at this point in the history
Blog routes export
  • Loading branch information
viethung7899 authored Nov 26, 2023
2 parents a98d3dc + 79d3944 commit bd7974a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
7 changes: 6 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const nextConfig = {
swcMinify: true,
output: "export",
images: {
domains: ["cdn.sanity.io"],
remotePatterns: [
{
hostname: "cdn.sanity.io",
},
],
unoptimized: true,
},
webpack: (config, { isServer }) => {
if (!isServer) {
Expand Down
11 changes: 4 additions & 7 deletions pages/blog/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { useLiveQuery } from "next-sanity/preview";
import { readToken } from "../api/sanity.api";
import { getClient } from "../api/sanity.client";
import { urlForImage } from "../api/sanity.image";
import { Helmet, HeaderNav, Footer } from "@components";
import { Helmet } from "@components";
import {
getPost,
postBySlugQuery,
postSlugsQuery,
} from "../api/sanity.queries";
import React from "react";
import { useRouter } from "next/router";
import clock from "../../public/images/blog-page/clock.svg";
import person from "../../public/images/blog-page/person.svg";
import { formatDate } from "utils/index";
Expand All @@ -37,15 +36,13 @@ export const getStaticProps = async ({ draftMode = false, params = {} }) => {
};

export default function ProjectSlugRoute(props) {
const router = useRouter();
const [post] = useLiveQuery(props.post, postBySlugQuery, {
slug: props.post.slug.current,
});

return (
<div className="blog-category-page">
<Helmet pageTitle={router.pathname} />
<HeaderNav />
<Helmet pageTitle={post.title} />
<main>
<header className="container hero">
{post.mainImage ? (
Expand Down Expand Up @@ -78,16 +75,16 @@ export default function ProjectSlugRoute(props) {
<PortableText value={post.body} />
</div>
</main>
<Footer />
</div>
);
}

export const getStaticPaths = async () => {
const client = getClient();
const slugs = await client.fetch(postSlugsQuery);
const paths = slugs.map((slug) => ({ params: { slug } }));
return {
paths: slugs ? slugs.map(({ slug }) => `/blog/${slug}`) : [],
paths,
fallback: false,
};
};
9 changes: 4 additions & 5 deletions pages/blog.js → pages/blog/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useLiveQuery } from "next-sanity/preview";

import Card from "components/Card";
import { readToken } from "./api/sanity.api";
import { getClient } from "./api/sanity.client";
import { getPosts, postsQuery } from "./api/sanity.queries";
import { readToken } from "../api/sanity.api";
import { getClient } from "../api/sanity.client";
import { getPosts, postsQuery } from "../api/sanity.queries";
import { Helmet, HeaderNav, Footer } from "@components";
import { useRouter } from "next/router";

Expand All @@ -23,10 +23,10 @@ export const getStaticProps = async ({ draftMode = false }) => {
export default function Post(props) {
const router = useRouter();
const [posts] = useLiveQuery(props.posts, postsQuery);

return (
<div className="blog-page">
<Helmet pageTitle={router.pathname} />
<HeaderNav />
<main>
<header className="container hero">
<p>Blog</p>
Expand All @@ -47,7 +47,6 @@ export default function Post(props) {
)}
</section>
</main>
<Footer />
</div>
);
}

0 comments on commit bd7974a

Please sign in to comment.