Skip to content

Commit 683c741

Browse files
committed
feat: use react page title and cache server pages
1 parent 2070e51 commit 683c741

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ over the data loading and give the users benefits of frontend cache data while t
151151
and forwards in the search page.
152152

153153
I have also used this on the movie details page. You could argue here that the frontend cache is not as useful
154-
as caching on the search page But should the users again go back and forth between search and movie details pages
155-
maybe view the same page movie details multiple times then this will feel a little more performant to the user.
156-
I also decide to be consistent across the two pages how their data is initialised and hydrated.
154+
as caching on the search page. I also decide to be consistent across the two pages in how their data is initialised and hydrated.
155+
So we have a standard data loading pattern.
156+
157+
Currently the Next.js Link component isn't always able to provide "Soft navigation",
158+
where routing is solely via the frontend rather instead a full page load - but once this problem is resolved the page could take full
159+
advantage of cache. In the meantime the page can be cached by setting the revalidate time of page.
160+
157161

158162
### Styling
159163

@@ -186,6 +190,7 @@ I had limited time so here are a few other things I would look at..
186190
- Storybook for individual components - this would actually make working on the components a breeze.
187191
- Better display for small screens - I would love to put more time into this, I think the search page could be optimised quite nicely for smaller screens
188192
- You could even add additional image sizes to the api endpoint and use different image sizes for different media queries
193+
- Pagination component could be more SEO friendly
189194
- Optimise our endpoints but reducing the payload size - there is currently data we send to the frontend that isn't used
190195
- It's not the prettiest web app (I'm not really a designer) again with a bit more time it would be good to put more polish on it terms of looks
191196
- Add husky to get checks running on push

src/app/movies/[movieId]/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { fetchMovieDetails } from '@/lib/fetch/fetchMovieDetails';
55
import MovieDetailsSection from '@/features/movies/MovieDetailsSection';
66
import { movieDetailQueryKey } from '@/lib/queryKeys';
77

8+
export const revalidate = 3600;
9+
810
const movieIdSlugSchema = z.string().regex(/^(\d+)-.+$/, 'Invalid movie slug format');
911

1012
type MoviePageProps = {

src/app/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { HomeSearchSection } from '@/features/home/HomeSearchSection';
44
import { getQueryClient } from '@/lib/getQueryClient';
55
import { moviesQueryKey } from '@/lib/queryKeys';
66

7+
export const revalidate = 300;
8+
79
interface HomePageProps {
810
searchParams: Promise<Record<string, string | undefined>>;
911
}

src/features/home/HomeSearchSection.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,6 @@ export function HomeSearchSection({ initialSearch, initialPage }: Props) {
5656
enabled: !!params.search,
5757
});
5858

59-
useEffect(() => {
60-
let title: string;
61-
if (params.search) {
62-
title =
63-
params.page && params.page > 1
64-
? `Search: ${params.search} (Page ${params.page}) | Movie Search`
65-
: `Search: ${params.search} | Movie Search`;
66-
} else {
67-
title = 'Movie Search';
68-
}
69-
document.title = title;
70-
}, [params.search, params.page]);
71-
7259
useEffect(() => {
7360
if (isSuccess && data) {
7461
lastTotalPagesRef.current = data.total_pages;
@@ -85,8 +72,15 @@ export function HomeSearchSection({ initialSearch, initialPage }: Props) {
8572

8673
const handlePageChange = (page: number) => setParams({ page });
8774

75+
const title = params.search
76+
? params.page && params.page > 1
77+
? `Search: ${params.search} (Page ${params.page}) | Movie Search`
78+
: `Search: ${params.search} | Movie Search`
79+
: 'Movie Search';
80+
8881
return (
8982
<section className="space-y-4">
83+
<title>{title}</title>
9084
<h1 className="text-xl font-bold text-stone-800">Welcome to Movie Search</h1>
9185
<p className="text-stone-600">
9286
Use the search box to find your favorite movies. Results will appear below.

src/features/movies/MovieDetailsSection.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default function MovieDetailsSection({ movieId }: Props) {
2626

2727
return (
2828
<section className="space-y-4">
29+
<title>{`Search for movies: ${data.title}`}</title>
2930
<BackToSearchLink />
3031
<MovieDetailsView movie={data} />
3132
</section>

0 commit comments

Comments
 (0)