Skip to content

Commit e2f502c

Browse files
committed
Remove id in urls
1 parent f6432ea commit e2f502c

File tree

6 files changed

+21
-18
lines changed

6 files changed

+21
-18
lines changed

src/components/AlgoliaSearch/AlgoliaSearchBox.component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const AlgoliaSearchBox = () => {
5151
}}
5252
/>
5353
{search && (
54-
<div className="absolute">
54+
<div className="absolute z-50 bg-white shadow-lg rounded-md mt-1 md:w-[18rem]">
5555
<Hits hitComponent={SearchResults} />
5656
</div>
5757
)}

src/components/AlgoliaSearch/SearchResults.component.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface ISearchResultProps {
1111
on_sale: boolean;
1212
short_description: string;
1313
objectID: number;
14+
slug: string;
1415
};
1516
}
1617

@@ -37,14 +38,10 @@ const SearchResults = ({
3738
objectID,
3839
},
3940
}: ISearchResultProps) => {
40-
// Replace empty spaces with dash (-)
41-
const trimmedProductName = product_name.replace(/ /g, '-');
42-
4341
return (
4442
<article className="cursor-pointer hit">
4543
<Link
46-
href="/produkt/[post]"
47-
as={`/produkt/${trimmedProductName}?id=${objectID}`}
44+
href={`/produkt/${product_name.replace(/ /g, '-')}`}
4845
passHref
4946
>
5047
<div className="flex p-6 bg-white">

src/components/Product/DisplayProducts.component.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
7979
return (
8080
<div key={uuidv4()} className="group">
8181
<Link
82-
href={`/produkt/${encodeURIComponent(
83-
slug,
84-
)}?id=${encodeURIComponent(databaseId)}`}
82+
href={`/produkt/${encodeURIComponent(slug)}`}
8583
>
8684
<div className="aspect-[3/4] relative overflow-hidden bg-gray-100">
8785
{image ? (
@@ -102,9 +100,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
102100
</div>
103101
</Link>
104102
<Link
105-
href={`/produkt/${encodeURIComponent(
106-
slug,
107-
)}?id=${encodeURIComponent(databaseId)}`}
103+
href={`/produkt/${encodeURIComponent(slug)}`}
108104
>
109105
<span>
110106
<div className="mt-4">

src/components/Product/ProductCard.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const ProductCard = ({
3333
return (
3434
<div className="group">
3535
<div className="aspect-[3/4] overflow-hidden bg-gray-100 relative">
36-
<Link href={`/produkt/${slug}?id=${databaseId}`}>
36+
<Link href={`/produkt/${slug}`}>
3737
{image?.sourceUrl ? (
3838
<Image
3939
src={image.sourceUrl}
@@ -51,7 +51,7 @@ const ProductCard = ({
5151
</Link>
5252
</div>
5353

54-
<Link href={`/produkt/${slug}?id=${databaseId}`}>
54+
<Link href={`/produkt/${slug}`}>
5555
<div className="mt-4">
5656
<p className="text-2xl font-bold text-center cursor-pointer hover:text-gray-600 transition-colors">
5757
{name}

src/pages/produkt/[slug].tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,21 @@ const Produkt: NextPage = ({
4848
export default withRouter(Produkt);
4949

5050
export const getServerSideProps: GetServerSideProps = async ({
51-
query: { id },
51+
params,
52+
query,
53+
res,
5254
}) => {
55+
// Handle legacy URLs with ID parameter by removing it
56+
if (query.id) {
57+
res.setHeader('Location', `/produkt/${params?.slug}`);
58+
res.statusCode = 301;
59+
res.end();
60+
return { props: {} };
61+
}
62+
5363
const { data, loading, networkStatus } = await client.query({
5464
query: GET_SINGLE_PRODUCT,
55-
variables: { id },
65+
variables: { slug: params?.slug },
5666
});
5767

5868
return {

src/utils/gql/GQL_QUERIES.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { gql } from '@apollo/client';
22

33
export const GET_SINGLE_PRODUCT = gql`
4-
query Product($id: ID!) {
5-
product(id: $id, idType: DATABASE_ID) {
4+
query Product($slug: ID!) {
5+
product(id: $slug, idType: SLUG) {
66
id
77
databaseId
88
averageRating

0 commit comments

Comments
 (0)