-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add scope to redirects #315
Open
jomunker
wants to merge
9
commits into
main
Choose a base branch
from
COM-878-scope-redirects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
73a06ff
feat(api): add redirect scope
jomunker 7fd69d2
feat(site): pass and use scope in middleware and redirects functions
jomunker ea1724c
feat(masterMenuData): add scopeParts to RedirectsPage
jomunker 35a92d9
Merge branch 'main' into COM-878-scope-redirects
jomunker 712c7d6
remove(pages-module): unnecessary RedirectScope from imports array
jomunker e9a0bb4
refactor(migration): add migration step to pre-existing migration fil…
jomunker b068920
Merge branch 'main' into COM-878-scope-redirects
jomunker a6c7eb9
refactor(middleware): move scope extraction after if to avoid undefin…
jomunker 1c863e5
Merge branch 'main' into COM-878-scope-redirects
jomunker 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
There are no files selected for viewing
This file contains 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 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 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 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 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,13 @@ | ||
import { Embeddable, Property } from "@mikro-orm/core"; | ||
import { Field, InputType, ObjectType } from "@nestjs/graphql"; | ||
import { IsString } from "class-validator"; | ||
|
||
@Embeddable() | ||
@ObjectType("RedirectScope") // name must not be changed in the app | ||
@InputType("RedirectScopeInput") // name must not be changed in the app | ||
export class RedirectScope { | ||
@Property({ columnType: "text" }) | ||
@Field() | ||
@IsString() | ||
domain: string; | ||
} |
This file contains 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 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,14 +1,15 @@ | ||||||
import { gql } from "@comet/cms-site"; | ||||||
import { ExternalLinkBlockData, InternalLinkBlockData, RedirectsLinkBlockData } from "@src/blocks.generated"; | ||||||
import { GQLRedirectScope } from "@src/graphql.generated"; | ||||||
import { createGraphQLFetch } from "@src/util/graphQLClient"; | ||||||
import { RouteHas } from "next/dist/lib/load-custom-routes"; | ||||||
|
||||||
import { memoryCache } from "./cache"; | ||||||
import { GQLRedirectsQuery, GQLRedirectsQueryVariables } from "./redirects.generated"; | ||||||
|
||||||
const redirectsQuery = gql` | ||||||
query Redirects($filter: RedirectFilter, $sort: [RedirectSort!], $offset: Int!, $limit: Int!) { | ||||||
paginatedRedirects(filter: $filter, sort: $sort, offset: $offset, limit: $limit) { | ||||||
query Redirects($scope: RedirectScopeInput!, $filter: RedirectFilter, $sort: [RedirectSort!], $offset: Int!, $limit: Int!) { | ||||||
paginatedRedirects(scope: $scope, filter: $filter, sort: $sort, offset: $offset, limit: $limit) { | ||||||
nodes { | ||||||
sourceType | ||||||
source | ||||||
|
@@ -33,7 +34,7 @@ const createInternalRedirects = async (): Promise<Map<string, Redirect>> => { | |||||
return redirectsMap; | ||||||
}; | ||||||
|
||||||
async function* fetchApiRedirects() { | ||||||
async function* fetchApiRedirects(scope: GQLRedirectScope) { | ||||||
let offset = 0; | ||||||
const limit = 100; | ||||||
|
||||||
|
@@ -43,6 +44,7 @@ async function* fetchApiRedirects() { | |||||
sort: { field: "createdAt", direction: "DESC" }, | ||||||
offset, | ||||||
limit, | ||||||
scope, | ||||||
}); | ||||||
|
||||||
yield* paginatedRedirects.nodes; | ||||||
|
@@ -55,14 +57,14 @@ async function* fetchApiRedirects() { | |||||
} | ||||||
} | ||||||
|
||||||
const createApiRedirects = async (): Promise<Map<string, Redirect>> => { | ||||||
const createApiRedirects = async (scope: GQLRedirectScope): Promise<Map<string, Redirect>> => { | ||||||
const redirects = new Map<string, Redirect>(); | ||||||
function replaceRegexCharacters(value: string): string { | ||||||
// escape ":" and "?", otherwise it is used for next.js regex path matching (https://nextjs.org/docs/pages/api-reference/next-config-js/redirects#regex-path-matching) | ||||||
return value.replace(/[:?]/g, "\\$&"); | ||||||
} | ||||||
|
||||||
for await (const redirect of fetchApiRedirects()) { | ||||||
for await (const redirect of fetchApiRedirects(scope)) { | ||||||
let source: string | undefined; | ||||||
let destination: string | undefined; | ||||||
let has: Redirect["has"]; | ||||||
|
@@ -113,9 +115,12 @@ const createApiRedirects = async (): Promise<Map<string, Redirect>> => { | |||||
|
||||||
type Redirect = { destination: string; permanent: boolean; has?: RouteHas[] | undefined }; | ||||||
|
||||||
export const createRedirects = async () => { | ||||||
const key = `redirects`; | ||||||
export const createRedirects = async (scope: GQLRedirectScope) => { | ||||||
const key = `redirects-${JSON.stringify(scope)}`; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return memoryCache.wrap(key, async () => { | ||||||
return new Map<string, Redirect>([...Array.from(await createApiRedirects()), ...Array.from(await createInternalRedirects())]); | ||||||
return new Map<string, Redirect>([ | ||||||
...Array.from(await createApiRedirects({ domain: scope.domain })), | ||||||
...Array.from(await createInternalRedirects()), | ||||||
]); | ||||||
}); | ||||||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is good practice to alter existing migrations. If the starter would be deployed, this change would not be made.
Create a new migration and also make it fail-safe by truncating the table before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#315 (comment)
Please discuss this with @johnnyomair and let me know what I should do ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, this was my suggestion 😅 We did this multiple times in the past to avoid having a bunch of migrations when starting a new project. We could change our approach though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dkarnutsch what do you prefer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nsams what do you think? Maybe we should start adding actually working migrations once the Starter has been deployed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A growing Number of Migrations in New Projects Is Not desirable, that is unnecessarily confusing.
Maybe we can drop database and run fixtures on every starter deploy?