Skip to content

Commit

Permalink
Merge branch 'main' into feature/III-6325
Browse files Browse the repository at this point in the history
  • Loading branch information
vhande committed Nov 6, 2024
2 parents 42d2032 + aec889f commit 78b7db5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CODEOWNERS are automatically assigned as possible reviewers to new PRs.

# Global owners (also need to be duplicated in later rules)
* @simon-debruijn @brampauwelyn @Anahkiasen
* @simon-debruijn @brampauwelyn @Anahkiasen @vhande

# Jenkins / deployment owners
Gemfile* @willaerk @paulherbosch
Expand Down
27 changes: 17 additions & 10 deletions src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,27 @@ const Layout = ({ children }) => {
useChangeLanguage();
useHandleWindowMessage({
[WindowMessageTypes.URL_CHANGED]: ({ path }) => {
const currentPath = window.location.pathname;
const url = new URL(
const currentUrl = new URL(window.location.href);
const nextUrl = new URL(
`${window.location.protocol}//${window.location.host}${path}`,
);
const query = Object.fromEntries(url.searchParams.entries());
const hasPage = url.searchParams.has('page');

const areUrlsDeepEqual =
currentUrl.pathname === nextUrl.pathname &&
[...nextUrl.searchParams.entries()]
.filter(([key]) => key !== 'jwt' && key !== 'lang')
.every(([key, val]) => currentUrl.searchParams.get(key) === val);

if (areUrlsDeepEqual) {
return;
}

const hasPage = nextUrl.searchParams.has('page');

if (hasPage) {
window.history.pushState(
undefined,
'',
`${window.location.protocol}//${window.location.host}${path}`,
);
window.history.pushState(undefined, '', nextUrl.toString());
} else {
router.push({ pathname: url.pathname, query });
router.push(`${nextUrl.pathname}${nextUrl.search}`);
}
},
[WindowMessageTypes.HTTP_ERROR_CODE]: ({ code }) => {
Expand Down
47 changes: 42 additions & 5 deletions src/middleware.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,51 @@ export const middleware = async (request: NextRequest) => {
return NextResponse.redirect(url);
}

const isOwnershipPage = request.nextUrl.pathname.endsWith('ownerships');
const isOwnershipPage =
(request.nextUrl.pathname.startsWith('/organizer') &&
!request.nextUrl.pathname.endsWith('/ownerships')) ||
request.nextUrl.pathname.startsWith('/search');

if (isOwnershipPage && !process.env.NEXT_PUBLIC_OWNERSHIP_ENABLED) {
const url = new URL('/404', request.url);
return NextResponse.redirect(url);
if (isOwnershipPage) {
const isOwnershipEnabled =
process.env.NEXT_PUBLIC_OWNERSHIP_ENABLED === 'true';

const redirectUrl = new URL(request.nextUrl);
// remove the path variables from nextjs routing
redirectUrl.searchParams.delete('params');

if (
isOwnershipEnabled &&
redirectUrl.searchParams.get('ownership') === 'true'
) {
return NextResponse.next();
}

if (
isOwnershipEnabled &&
redirectUrl.searchParams.get('ownership') !== 'true'
) {
redirectUrl.searchParams.set('ownership', 'true');
return NextResponse.redirect(redirectUrl);
}

if (!isOwnershipEnabled && redirectUrl.searchParams.has('ownership')) {
redirectUrl.searchParams.delete('ownership');
return NextResponse.redirect(redirectUrl);
}

return NextResponse.next();
}
};

export const config = {
matcher: ['/event', '/login', '/organizers/:id/ownerships'],
matcher: [
'/event',
'/login',
'/organizers/:id/ownerships',
'/organizer/(.*)',
'/(.*)/ownerships',
'/search(.*)',
'/[...params]',
],
};
31 changes: 28 additions & 3 deletions src/pages/[...params].page.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Fallback = () => {
const router = useRouter();
const { asPath } = router;

const legacyPath = useLegacyPath();
const { publicRuntimeConfig } = getConfig();

// Keep track of which paths were not found. Do not store as a single boolean
// for the current path, because it's possible to navigate from a 404 path to
Expand All @@ -42,12 +42,37 @@ const Fallback = () => {
const [notFoundPaths, setNotFoundPaths] = useState(['/404']);
useHandleWindowMessage({
[WindowMessageTypes.URL_UNKNOWN]: () =>
setNotFoundPaths([asPath, ...notFoundPaths]),
setNotFoundPaths([router.asPath, ...notFoundPaths]),
});

const isClientSide = useIsClient();

if (notFoundPaths.includes(asPath)) {
const { cookies } = useCookiesWithOptions(['token', 'udb-language']);
const token = cookies['token'];
const language = cookies['udb-language'];

const legacyPath = useMemo(() => {
const path = new URL(`http://localhost${router.asPath}`).pathname;
const { params = [], ...queryWithoutParams } = router.query;
const queryString = prefixWhenNotEmpty(
new URLSearchParams({
...queryWithoutParams,
jwt: token,
lang: language,
}),
'?',
);

return `${publicRuntimeConfig.legacyAppUrl}${path}${queryString}`;
}, [
language,
publicRuntimeConfig.legacyAppUrl,
router.asPath,
router.query,
token,
]);

if (notFoundPaths.includes(router.asPath)) {
return <PageNotFound />;
}

Expand Down

0 comments on commit 78b7db5

Please sign in to comment.