Skip to content

Commit bbbf3ba

Browse files
authored
fix: internal links (#292)
1 parent 28ebf42 commit bbbf3ba

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

apps/blog-bff/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ app.route('/authors', authors);
1515
app.route('/newsletter', newsletter);
1616

1717
app.onError((err, c) => {
18+
console.error(err);
1819
if (err instanceof HTTPException) {
1920
return err.getResponse();
2021
}

libs/blog-bff/articles/api/src/lib/utils.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,58 @@ export const removeEmptyParagraphs: RewriteAdapter = ($) => {
8787
});
8888
};
8989

90+
/**
91+
* Transforms legacy links to new ones
92+
* @param url
93+
*/
94+
function transformUrl(url: string): URL {
95+
const parsedUrl = new URL(url);
96+
97+
const hostnamesToModify = ['wp.angular.love', 'replica.angular.love'];
98+
99+
const matchedHostname = hostnamesToModify.find(
100+
(hostname) => hostname === parsedUrl.hostname,
101+
);
102+
103+
if (matchedHostname) {
104+
parsedUrl.hostname = 'angular.love';
105+
106+
// Regex pattern to match optional language code and /{year}/{month}/{day}/{slug}/ structure
107+
const dateSlugPattern =
108+
/^(\/[a-z]{2})?\/(\d{4})\/(\d{2})\/(\d{2})\/([^/]+)\/?$/;
109+
110+
const match = parsedUrl.pathname.match(dateSlugPattern);
111+
112+
if (match) {
113+
// If the pattern matches, extract the language code (if present) and the slug
114+
const [, langCode, , , , slug] = match;
115+
116+
if (langCode) {
117+
parsedUrl.pathname = `${langCode}/${slug}`;
118+
} else {
119+
parsedUrl.pathname = `/${slug}`;
120+
}
121+
}
122+
}
123+
124+
return parsedUrl;
125+
}
126+
90127
/**
91128
* Appends aria-label and target attributes to links
92129
* @param $
93130
*/
94131
export const modifyLinks: RewriteAdapter = ($) => {
95132
$('a').each((_, element) => {
96133
const $element = $(element);
97-
$element.attr('target', `_blank`);
134+
$element.attr('target', '_blank');
98135

99136
if ($element.attr('href') && !$element.attr('href').startsWith('#')) {
100-
const { hostname } = new URL($element.attr('href'));
101-
$element.attr('aria-label', `Read more on ${hostname}`);
137+
const originalHref = $element.attr('href');
138+
const transformedURL = transformUrl(originalHref);
139+
$element.attr('href', transformedURL.toString());
140+
141+
$element.attr('aria-label', `Read more on ${transformedURL.hostname}`);
102142
}
103143
});
104144
};
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import { env } from 'hono/adapter';
12
import { cache } from 'hono/cache';
23
import { createMiddleware } from 'hono/factory';
34

45
import { getWpLang } from './lang';
56

6-
export const appCache = createMiddleware(
7-
cache({
7+
export const appCache = createMiddleware((c, next) => {
8+
if (env<{ DISABLE_CACHE: string }>(c)['DISABLE_CACHE']) {
9+
return next();
10+
}
11+
return cache({
812
wait: false,
913
cacheName: 'al-bff',
1014
cacheControl: 'max-age=3600',
1115
keyGenerator: (c) => `${c.req.url}_${getWpLang(c, 'en')}`,
1216
vary: 'x-al-lang',
13-
}),
14-
);
17+
})(c, next);
18+
});

0 commit comments

Comments
 (0)