MCPcopy Create free account
hub / github.com/Linen-dev/linen.dev / rewrite

Function rewrite

apps/web/utilities/middlewareHelper.ts:42–105  ·  view source on GitHub ↗
({
  hostname,
  pathname,
  url,
  userAgent,
}: {
  hostname: string | null;
  pathname: string;
  url: NextURL;
  userAgent: string | null;
})

Source from the content-addressed store, hash-verified

40 hostname.indexOf('linen.dev') > -1 ? 'www.linen.dev' : hostname;
41
42export function rewrite({
43 hostname,
44 pathname,
45 url,
46 userAgent,
47}: {
48 hostname: string | null;
49 pathname: string;
50 url: NextURL;
51 userAgent: string | null;
52}) {
53 function isLocalIpAddress(hostname: string | null) {
54 if (!hostname) {
55 return false;
56 }
57 return hostname.startsWith('192.168.1.');
58 }
59 if (isLocalIpAddress(hostname)) {
60 return;
61 }
62
63 if (pathname === '/sitemap.xml') {
64 return {
65 rewrite: `${LINEN_STATIC_CDN}/sitemap/${cleanLinenHost(
66 hostname || 'linen.dev'
67 )}/sitemap.xml`,
68 };
69 }
70 if (pathname === '/robots.txt') {
71 return {
72 rewrite: `${LINEN_STATIC_CDN}/sitemap/${cleanLinenHost(
73 hostname || 'linen.dev'
74 )}/robots.txt`,
75 };
76 }
77
78 if (pathname.startsWith('/s/') && isBot(userAgent || '')) {
79 url.pathname = url.pathname.replace('/s/', '/ssr/');
80 return { rewrite: url.toString() };
81 }
82
83 if (!isSubdomainbasedRouting(hostname || '')) {
84 return;
85 }
86
87 //Community name is the subdomain of the request or the full url if it's a redirect
88 const communityName = getCommunityName(IS_PRODUCTION, hostname);
89
90 if (!isTopLevelPathname(pathname) && communityName !== '') {
91 url.pathname = `/s/${communityName}${pathname}`;
92 url.searchParams.append('customDomain', '1');
93
94 if (isBot(userAgent || '')) {
95 url.pathname = url.pathname.replace('/s/', '/ssr/');
96 return { rewrite: url.toString() };
97 }
98
99 return { rewrite: url.toString() };

Callers 2

middlewareFunction · 0.90

Calls 6

isBotFunction · 0.90
isSubdomainbasedRoutingFunction · 0.90
isLocalIpAddressFunction · 0.85
cleanLinenHostFunction · 0.85
isTopLevelPathnameFunction · 0.85
getCommunityNameFunction · 0.70

Tested by

no test coverage detected