Skip to content

Commit f7416d0

Browse files
authored
Merge pull request #1447 from w3bdesign/1445-urls-slug-on-product-and-category-pages
Remove id in urls
2 parents f6432ea + 4431074 commit f7416d0

File tree

7 files changed

+27
-34
lines changed

7 files changed

+27
-34
lines changed

src/components/AlgoliaSearch/AlgoliaSearchBox.component.tsx

+1-1
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

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Link from 'next/link';
2-
32
import { trimmedStringToLength } from '@/utils/functions/functions';
43

54
interface ISearchResultProps {
@@ -10,7 +9,7 @@ interface ISearchResultProps {
109
sale_price: string;
1110
on_sale: boolean;
1211
short_description: string;
13-
objectID: number;
12+
slug: string;
1413
};
1514
}
1615

@@ -23,7 +22,6 @@ interface ISearchResultProps {
2322
* @param {string} sale_price Price when on sale
2423
* @param {boolean} on_sale Is the product on sale? True or false
2524
* @param {string} short_description Short description of product
26-
* @param {number} objectID ID of product
2725
}
2826
*/
2927
const SearchResults = ({
@@ -34,17 +32,12 @@ const SearchResults = ({
3432
sale_price,
3533
on_sale,
3634
short_description,
37-
objectID,
3835
},
3936
}: ISearchResultProps) => {
40-
// Replace empty spaces with dash (-)
41-
const trimmedProductName = product_name.replace(/ /g, '-');
42-
4337
return (
4438
<article className="cursor-pointer hit">
4539
<Link
46-
href="/produkt/[post]"
47-
as={`/produkt/${trimmedProductName}?id=${objectID}`}
40+
href={`/produkt/${product_name.replace(/ /g, '-')}`}
4841
passHref
4942
>
5043
<div className="flex p-6 bg-white">

src/components/Product/DisplayProducts.component.tsx

+6-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ interface Variations {
2323

2424
interface RootObject {
2525
__typename: string;
26-
databaseId: number;
2726
name: string;
2827
onSale: boolean;
2928
slug: string;
@@ -55,7 +54,6 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
5554
{products ? (
5655
products.map(
5756
({
58-
databaseId,
5957
name,
6058
price,
6159
regularPrice,
@@ -78,11 +76,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
7876

7977
return (
8078
<div key={uuidv4()} className="group">
81-
<Link
82-
href={`/produkt/${encodeURIComponent(
83-
slug,
84-
)}?id=${encodeURIComponent(databaseId)}`}
85-
>
79+
<Link href={`/produkt/${encodeURIComponent(slug)}`}>
8680
<div className="aspect-[3/4] relative overflow-hidden bg-gray-100">
8781
{image ? (
8882
<img
@@ -96,16 +90,14 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
9690
id="product-image"
9791
className="w-full h-full object-cover object-center transition duration-300 group-hover:scale-105"
9892
alt={name}
99-
src={process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL}
93+
src={
94+
process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL
95+
}
10096
/>
10197
)}
10298
</div>
10399
</Link>
104-
<Link
105-
href={`/produkt/${encodeURIComponent(
106-
slug,
107-
)}?id=${encodeURIComponent(databaseId)}`}
108-
>
100+
<Link href={`/produkt/${encodeURIComponent(slug)}`}>
109101
<span>
110102
<div className="mt-4">
111103
<p className="text-2xl font-bold text-center cursor-pointer hover:text-gray-600 transition-colors">
@@ -127,9 +119,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
127119
</span>
128120
</div>
129121
) : (
130-
<span className="text-lg text-gray-900">
131-
{price}
132-
</span>
122+
<span className="text-lg text-gray-900">{price}</span>
133123
)}
134124
</div>
135125
</div>

src/components/Product/ProductCard.component.tsx

+2-2
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/components/User/UserRegistration.component.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const UserRegistration = () => {
3636
} else {
3737
throw new Error('Failed to register customer');
3838
}
39-
} catch (err: unknown) {
40-
console.error('Registration error');
39+
} catch (error: unknown) {
40+
console.error('Registration error:', error);
4141
}
4242
};
4343

src/pages/produkt/[slug].tsx

+12-2
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

+2-2
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)