Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import { useQuery } from '@tanstack/react-query';
import { Card, Text } from 'opub-ui';

import { GraphQLPublic } from '@/lib/api';
import {
getPlatformRootUrl,
isCollaborativeSubdomainHost as isCollaborativeSubdomainHostname,
} from '@/lib/collaborativesRouting';
import { isCollaborativeSubdomainHost as isCollaborativeSubdomainHostname } from '@/lib/collaborativesRouting';
import { formatDate, generateJsonLd } from '@/lib/utils';
import BreadCrumbs from '@/components/BreadCrumbs';
import { Icons } from '@/components/icons';
Expand Down Expand Up @@ -325,10 +322,14 @@ const CollaborativeDetailClient = () => {
},
});

const platformRootUrl = getPlatformRootUrl();
const organizationPublisherHref = (org: any) => {
const path = `/publishers/organization/${org.slug + '_' + org.id}`;
return platformRootUrl === '/' ? path : `${platformRootUrl}${path}`;
// Match getPlatformEntityUrl() behavior (absolute to platform host + locale)
const platformBaseUrl = (
process.env.NEXT_PUBLIC_PLATFORM_URL || ''
).replace(/\/$/, '');
const localeSegment = locale ? `/${locale}` : '';
return platformBaseUrl ? `${platformBaseUrl}${localeSegment}${path}` : path;
};

return (
Expand Down
13 changes: 13 additions & 0 deletions app/[locale]/(user)/collaboratives/components/Metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,21 @@ const Metadata = ({ data, setOpen }: { data: any; setOpen?: any }) => {
: '/org.png';

const platformRootUrl = getPlatformRootUrl();
const getLocaleSegment = () => {
if (typeof window === 'undefined') return '';
const match = window.location.pathname.match(/^\/([a-z]{2})(\/|$)/i);
return match ? `/${match[1].toLowerCase()}` : '';
};
const contributorHref = (contributor: any) => {
const path = `/publishers/${contributor.fullName + '_' + contributor.id}`;
// Match getPlatformEntityUrl() behavior (absolute to NEXT_PUBLIC_PLATFORM_URL + locale)
const platformBaseUrl = (process.env.NEXT_PUBLIC_PLATFORM_URL || '').replace(
/\/$/,
''
);
const localeSegment = getLocaleSegment();
if (platformBaseUrl) return `${platformBaseUrl}${localeSegment}${path}`;

return platformRootUrl === '/' ? path : `${platformRootUrl}${path}`;
};

Expand Down
19 changes: 17 additions & 2 deletions app/[locale]/dashboard/components/CollaborativeSubdomainNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import Image from 'next/image';
import Link from 'next/link';
import { useParams } from 'next/navigation';
import { Text } from 'opub-ui';
import { getPlatformRootUrl } from '@/lib/collaborativesRouting';

type Props = {
useCasesTargetId?: string;
Expand All @@ -20,11 +20,26 @@ export function CollaborativeSubdomainNav({
useCasesTargetId = 'collaborative-use-cases',
datasetsTargetId = 'collaborative-datasets',
}: Props) {
const params = useParams();
const locale =
typeof (params as any)?.locale === 'string'
? (params as any).locale
: undefined;

const platformBaseUrl = (process.env.NEXT_PUBLIC_PLATFORM_URL || '').replace(
/\/$/,
''
);
const localeSegment = locale ? `/${locale}` : '';
const listingHref = platformBaseUrl
? `${platformBaseUrl}${localeSegment}/collaboratives`
: `${localeSegment}/collaboratives`;

return (
<nav className="z-50 sticky top-1 min-h-[80px] border-b-1 border-solid border-baseGraySlateSolid9 px-4 py-6 sm:py-8 md:py-5 lg:px-1 lg:py-3">
<div className=" flex items-center justify-between gap-4">
<div className="flex items-center gap-1">
<Link href={getPlatformRootUrl()}>
<Link href={listingHref}>
<div className="flex items-center gap-2">
<div className="group relative h-[35px] w-[130px] overflow-hidden md:h-[40px] md:w-[150px] lg:h-[68px] lg:w-[183px]">
<div className="absolute inset-0">
Expand Down
18 changes: 12 additions & 6 deletions lib/collaborativesRouting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ export const isCollaborativeSubdomainHost = (

let slug: string | null = null;

if (domain === 'localhost' || domain === '127.0.0.1') {
const localSuffix = domain === 'localhost' ? '.localhost' : '.127.0.0.1';
// Local development support: <slug>.collab.localhost
if (domain === 'collab.localhost' || domain === 'collab.127.0.0.1') {
const localSuffix =
domain === 'collab.localhost' ? '.collab.localhost' : '.collab.127.0.0.1';
if (!currentHost.endsWith(localSuffix)) return false;
slug = currentHost.slice(0, -localSuffix.length);
} else {
Expand All @@ -117,9 +119,12 @@ export const getCollaborativeDetailUrl = (slug?: string | null) => {
const protocol = getConfiguredProtocol();
const localePrefix = getCurrentLocalePrefix();

if (domain === 'localhost' || domain === '127.0.0.1') {
if (domain === 'collab.localhost' || domain === 'collab.127.0.0.1') {
const portSuffix = getConfiguredPortSuffix();
return `${protocol}//${slug}.localhost${portSuffix}${localePrefix}`;
console.log('portSuffix', portSuffix);
const host =
domain === 'collab.127.0.0.1' ? 'collab.127.0.0.1' : 'collab.localhost';
return `${protocol}//${slug}.${host}${portSuffix}${localePrefix}`;
}
return `${protocol}//${slug}.${domain}${localePrefix}`;
};
Expand All @@ -131,9 +136,10 @@ export const getPlatformRootUrl = () => {
const protocol = getConfiguredProtocol();
const localePrefix = getCurrentLocalePrefix();

if (domain === 'localhost' || domain === '127.0.0.1') {
// Local dev: keep port (e.g. collab.localhost:3000)
if (domain === 'collab.localhost' || domain === 'collab.127.0.0.1') {
const portSuffix = getConfiguredPortSuffix();
return `${protocol}//localhost${portSuffix}${localePrefix}`;
return `${protocol}//${domain}${portSuffix}${localePrefix}`;
}

return `${protocol}//${domain}${localePrefix}`;
Expand Down
28 changes: 25 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ const getCollaborativeSlugFromHostname = (hostname: string) => {
'cdn',
]);

// Local development support: <slug>.localhost
if (baseHostname === 'localhost' || baseHostname === '127.0.0.1') {
// Local: <slug>.collab.localhost OR <slug>.collab.127.0.0.1
if (
baseHostname === 'collab.localhost' ||
baseHostname === 'collab.127.0.0.1'
) {
const localSuffix =
baseHostname === 'localhost' ? '.localhost' : '.127.0.0.1';
baseHostname === 'collab.localhost'
? '.collab.localhost'
: '.collab.127.0.0.1';
if (!normalizedHost.endsWith(localSuffix)) return null;
const slug = normalizedHost.slice(0, -localSuffix.length);
if (!slug || slug.includes('.')) return null;
Expand All @@ -119,6 +124,23 @@ export default function middleware(req: NextRequest) {
const localePathRegex = RegExp(`^/(${locales.all.join('|')})(/.*)$`, 'i');
const localePathMatch = req.nextUrl.pathname.match(localePathRegex);

const baseHostname = getBaseHostname();
const isPlatformHost = Boolean(baseHostname) && hostname === baseHostname;

// Ensure collab.<domain>/ shows collaboratives listing (not main homepage)
if (
isPlatformHost &&
(req.nextUrl.pathname === '/' || Boolean(localeRootMatch))
) {
const rewriteUrl = req.nextUrl.clone();
const defaultLocalePrefix = `/${locales.default}`;
const localePrefix = localeRootMatch
? `/${localeRootMatch[1]}`
: defaultLocalePrefix;
rewriteUrl.pathname = `${localePrefix}/collaboratives`;
return NextResponse.rewrite(rewriteUrl);
}

const collaborativeSlug = getCollaborativeSlugFromHostname(hostname);
if (collaborativeSlug) {
const rewriteUrl = req.nextUrl.clone();
Expand Down
Loading