-
Notifications
You must be signed in to change notification settings - Fork 2
Enhance project caching #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
792d1d1
Enhance project caching
oscarlund121 0b8f984
copolit-comments updates
oscarlund121 2777b8b
route.ts - trailing spaces
oscarlund121 d61281f
refresfed state sset to false
oscarlund121 02dfb39
Improves API caching and error handling
oscarlund121 bc99998
post til refreshProjects
oscarlund121 ff9d727
Updates fetch request to use POST method
oscarlund121 315a43e
Use refreshing state to show loading
ulrikandersen 42ee244
Removes unused ref and simplifies project refresh logic
oscarlund121 e1a2a93
Refactors project API endpoints to support refresh logic
oscarlund121 d577e9d
Refactors project data handling and API structure
oscarlund121 2a2f1e1
Handles decode errors as missing cache
oscarlund121 a9a64de
complerte removal of catch error
oscarlund121 f1c4d29
Removes unused setProjects function from context
oscarlund121 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,39 @@ | ||
| import { redirect } from "next/navigation" | ||
| import { SessionProvider } from "next-auth/react" | ||
| import { session, projectRepository } from "@/composition" | ||
| import ErrorHandler from "@/common/ui/ErrorHandler" | ||
| import SessionBarrier from "@/features/auth/view/SessionBarrier" | ||
| import ProjectsContextProvider from "@/features/projects/view/ProjectsContextProvider" | ||
| import { SidebarTogglableContextProvider, SplitView } from "@/features/sidebar/view" | ||
| import { redirect } from "next/navigation"; | ||
| import { SessionProvider } from "next-auth/react"; | ||
| import { session } from "@/composition"; | ||
| import ErrorHandler from "@/common/ui/ErrorHandler"; | ||
| import SessionBarrier from "@/features/auth/view/SessionBarrier"; | ||
| import ProjectsContextProvider from "@/features/projects/view/ProjectsContextProvider"; | ||
| import { projectDataSource } from "@/composition"; | ||
| import { | ||
| SidebarTogglableContextProvider, | ||
| SplitView, | ||
| } from "@/features/sidebar/view"; | ||
|
|
||
| export default async function Layout({ children }: { children: React.ReactNode }) { | ||
| const isAuthenticated = await session.getIsAuthenticated() | ||
| export default async function Layout({ | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| }) { | ||
| const isAuthenticated = await session.getIsAuthenticated(); | ||
| if (!isAuthenticated) { | ||
| return redirect("/api/auth/signin") | ||
| return redirect("/api/auth/signin"); | ||
| } | ||
| const projects = await projectRepository.get() | ||
|
|
||
| const projects = await projectDataSource.getProjects(); | ||
|
|
||
|
|
||
| return ( | ||
| <ErrorHandler> | ||
| <SessionProvider> | ||
| <SessionBarrier> | ||
| <ProjectsContextProvider initialProjects={projects}> | ||
| <SidebarTogglableContextProvider> | ||
| <SplitView> | ||
| {children} | ||
| </SplitView> | ||
| <SplitView>{children}</SplitView> | ||
| </SidebarTogglableContextProvider> | ||
| </ProjectsContextProvider> | ||
| </SessionBarrier> | ||
| </SessionProvider> | ||
| </ErrorHandler> | ||
| ) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,41 @@ | ||
| import { NextRequest, NextResponse } from "next/server" | ||
| import { session, userGitHubClient } from "@/composition" | ||
| import { makeUnauthenticatedAPIErrorResponse } from "@/common" | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { session, userGitHubClient } from "@/composition"; | ||
| import { makeUnauthenticatedAPIErrorResponse } from "@/common"; | ||
|
|
||
| interface GetBlobParams { | ||
| owner: string | ||
| repository: string | ||
| path: [string] | ||
| owner: string; | ||
| repository: string; | ||
| path: [string]; | ||
| } | ||
|
|
||
| export async function GET(req: NextRequest, { params }: { params: Promise<GetBlobParams> }) { | ||
| const isAuthenticated = await session.getIsAuthenticated() | ||
| export async function GET( | ||
| req: NextRequest, | ||
| { params }: { params: Promise<GetBlobParams> } | ||
| ) { | ||
| const isAuthenticated = await session.getIsAuthenticated(); | ||
| if (!isAuthenticated) { | ||
| return makeUnauthenticatedAPIErrorResponse() | ||
| return makeUnauthenticatedAPIErrorResponse(); | ||
| } | ||
| const { path: paramsPath, owner, repository } = await params | ||
| const path = paramsPath.join("/") | ||
| const { path: paramsPath, owner, repository } = await params; | ||
| const path = paramsPath.join("/"); | ||
| const item = await userGitHubClient.getRepositoryContent({ | ||
| repositoryOwner: owner, | ||
| repositoryName: repository, | ||
| path: path, | ||
| ref: req.nextUrl.searchParams.get("ref") ?? undefined | ||
| }) | ||
| const url = new URL(item.downloadURL) | ||
| ref: req.nextUrl.searchParams.get("ref") ?? undefined, | ||
| }); | ||
| const url = new URL(item.downloadURL); | ||
| const imageRegex = /\.(jpg|jpeg|png|webp|avif|gif)$/; | ||
| const file = await fetch(url).then(r => r.blob()) | ||
| const headers = new Headers() | ||
| const res = await fetch(url); | ||
| const file = await res.blob(); | ||
|
|
||
| const headers = new Headers(); | ||
| if (new RegExp(imageRegex).exec(path)) { | ||
| const cacheExpirationInSeconds = 60 * 60 * 24 * 30 // 30 days | ||
| const cacheExpirationInSeconds = 60 * 60 * 24 * 30; // 30 days | ||
| headers.set("Content-Type", "image/*"); | ||
| headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`) | ||
| headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`); | ||
| } else { | ||
oscarlund121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| headers.set("Content-Type", "text/plain"); | ||
| } | ||
| return new NextResponse(file, { status: 200, headers }) | ||
| return new NextResponse(file, { status: 200, headers }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { NextResponse } from "next/server" | ||
| import { projectDataSource } from "@/composition" | ||
|
|
||
| export async function GET() { | ||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const projects = await projectDataSource.refreshProjects() | ||
| return NextResponse.json({ projects }) | ||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
oscarlund121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,4 +230,4 @@ export const gitHubHookHandler = new GitHubHookHandler({ | |
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,4 +75,4 @@ const ElementsAPI = ({ | |
| }) | ||
| } | ||
|
|
||
| export default Stoplight | ||
| export default Stoplight | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,4 +62,4 @@ const Swagger = ({ url }: { url: string }) => { | |
| ) | ||
| } | ||
|
|
||
| export default Swagger | ||
| export default Swagger | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,35 @@ | ||
| import Project from "./Project" | ||
| import IProjectDataSource from "./IProjectDataSource" | ||
| import IProjectRepository from "./IProjectRepository" | ||
| import Project from "./Project"; | ||
| import IProjectDataSource from "./IProjectDataSource"; | ||
| import IProjectRepository from "./IProjectRepository"; | ||
| import { revalidatePath } from "next/cache"; | ||
|
|
||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| export default class CachingProjectDataSource implements IProjectDataSource { | ||
| private dataSource: IProjectDataSource | ||
| private repository: IProjectRepository | ||
|
|
||
| constructor(config: { dataSource: IProjectDataSource, repository: IProjectRepository }) { | ||
| this.dataSource = config.dataSource | ||
| this.repository = config.repository | ||
| private dataSource: IProjectDataSource; | ||
| private repository: IProjectRepository; | ||
|
|
||
| constructor(config: { | ||
| dataSource: IProjectDataSource; | ||
| repository: IProjectRepository; | ||
| }) { | ||
| this.dataSource = config.dataSource; | ||
| this.repository = config.repository; | ||
| } | ||
|
|
||
| async getProjects(): Promise<Project[]> { | ||
| const projects = await this.dataSource.getProjects() | ||
| await this.repository.set(projects) | ||
| return projects | ||
| const cache = await this.repository.get(); | ||
|
|
||
| if (cache) return cache; | ||
| else { | ||
| const projects = await this.dataSource.getProjects(); | ||
| await this.repository.set(projects); | ||
|
|
||
| return projects; | ||
| } | ||
| } | ||
|
|
||
| async refreshProjects(): Promise<Project[]> { | ||
| const projects = await this.dataSource.getProjects(); | ||
| await this.repository.set(projects); | ||
| return projects; | ||
| } | ||
ulrikandersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,72 @@ | ||
| "use client" | ||
| "use client"; | ||
|
|
||
| import { useState } from "react" | ||
| import { ProjectsContext } from "@/common" | ||
| import { Project } from "@/features/projects/domain" | ||
| import { useState, useEffect } from "react"; | ||
| import { ProjectsContext } from "@/common"; | ||
| import { Project } from "@/features/projects/domain"; | ||
|
|
||
| const ProjectsContextProvider = ({ | ||
| initialProjects, | ||
| children | ||
| children, | ||
| }: { | ||
| initialProjects?: Project[], | ||
| children?: React.ReactNode | ||
| initialProjects?: Project[]; | ||
| children?: React.ReactNode; | ||
| }) => { | ||
| const [refreshed, setRefreshed] = useState<boolean>(false) | ||
| const [projects, setProjects] = useState<Project[]>(initialProjects || []) | ||
| const [refreshed, setRefreshed] = useState<boolean>(false); | ||
| const [projects, setProjects] = useState<Project[]>(initialProjects || []); | ||
|
|
||
| const hasProjectChanged = (value: Project[]) => value.some((project, index) => { | ||
| // Compare by project id and version (or any other key fields) | ||
| return project.id !== projects[index]?.id || project.versions !== projects[index]?.versions | ||
| }) | ||
| const hasProjectChanged = (value: Project[]) => | ||
| value.some((project, index) => { | ||
| // Compare by project id and version (or any other key fields) | ||
| return ( | ||
| project.id !== projects[index]?.id || | ||
| project.versions !== projects[index]?.versions | ||
| ); | ||
| }); | ||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const setProjectsAndRefreshed = (value: Project[]) => { | ||
| setProjects(value) | ||
| setProjects(value); | ||
| // If any project has changed, update the state and mark as refreshed | ||
| if (hasProjectChanged(value)) setRefreshed(true) | ||
| if (hasProjectChanged(value)) setRefreshed(true); | ||
| }; | ||
|
|
||
| // Trigger background refresh after initial mount | ||
| useEffect(() => { | ||
| const refreshProjects = () => { | ||
| fetch("/api/projects") | ||
| .then((res) => res.json()) | ||
| .then( | ||
| ({ projects }) => | ||
| projects && | ||
| hasProjectChanged(projects) && | ||
| setProjectsAndRefreshed(projects) | ||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| .catch((error) => console.log("Failed to refresh projects", error)); | ||
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
oscarlund121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| // Initial refresh | ||
| refreshProjects(); | ||
|
|
||
| // Refresh when tab becomes active | ||
| const handleVisibilityChange = () => { | ||
| if (!document.hidden) refreshProjects(); | ||
oscarlund121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| document.addEventListener("visibilitychange", handleVisibilityChange); | ||
| return () => | ||
| document.removeEventListener("visibilitychange", handleVisibilityChange); | ||
| }, []); | ||
oscarlund121 marked this conversation as resolved.
Show resolved
Hide resolved
oscarlund121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
| return ( | ||
| <ProjectsContext.Provider value={{ | ||
| refreshed, | ||
| projects, | ||
| setProjects: setProjectsAndRefreshed | ||
| }}> | ||
| <ProjectsContext.Provider | ||
| value={{ | ||
| refreshed, | ||
| projects, | ||
| setProjects: setProjectsAndRefreshed, | ||
| }} | ||
| > | ||
| {children} | ||
| </ProjectsContext.Provider> | ||
| ) | ||
| } | ||
| ); | ||
| }; | ||
|
|
||
| export default ProjectsContextProvider | ||
| export default ProjectsContextProvider; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.