Skip to content

Commit

Permalink
🔍 Improve SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
segersniels committed Aug 5, 2024
1 parent f24b87f commit 0dabb5a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
50 changes: 50 additions & 0 deletions apps/web/app/routes/robots[.]txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { LoaderFunctionArgs } from '@remix-run/cloudflare';

enum PolicyType {
UserAgent = 'User-agent',
Allow = 'Allow',
Disallow = 'Disallow',
Sitemap = 'Sitemap',
CrawlDelay = 'Crawl-delay',
}

type Policy = {
type: PolicyType;
value: string;
};

function getRobotsText(policies: Policy[]): string {
let result = '';
for (const { type, value } of policies) {
result += `${type}: ${value}\n`;
}

return result;
}

export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
const baseUrl = `${url.protocol}//${url.host}`;
const robots = getRobotsText([
{
type: PolicyType.UserAgent,
value: '*',
},
{
type: PolicyType.Allow,
value: '/',
},
{
type: PolicyType.Sitemap,
value: `${baseUrl}/sitemap.xml`,
},
]);

return new Response(robots, {
headers: {
'Content-Type': 'text/plain',
'Cache-Control': 'public, max-age=86400, must-revalidate',
'Content-Length': new TextEncoder().encode(robots).byteLength.toString(),
},
});
}
21 changes: 21 additions & 0 deletions apps/web/app/routes/sitemap[.]xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LoaderFunctionArgs } from '@remix-run/cloudflare';

export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
const baseUrl = `${url.protocol}//${url.host}`;

const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>${baseUrl}</loc></url>
<url><loc>${baseUrl}/web</loc></url>
</urlset>
`;

return new Response(sitemap, {
status: 200,
headers: {
'Content-Type': 'application/xml',
'Cache-Control': 'public, max-age=86400, must-revalidate',
},
});
}
3 changes: 3 additions & 0 deletions apps/web/public/_headers
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
https://:project.pages.dev/*
X-Robots-Tag: noindex

/favicon.ico
Cache-Control: public, max-age=3600, s-maxage=3600
/assets/*
Expand Down

0 comments on commit 0dabb5a

Please sign in to comment.