Exposing assets with loader functions
Earlier in this chapter, we created a robots.txt
file and exposed it by placing it into the public
folder. However, we can also use loader
functions in resource routes to expose assets. This is particularly useful when we want to dynamically create these assets on the fly or manage user access. To get started, follow these steps:
- Delete the existing
robots.txt
file from the public folder as it will override our API route otherwise. - Create a new
robots[.txt].tsx
file in the routes folder.The square brackets let us escape parts of the route name. Instead of creating a
.txt
file, we create a.tsx
file but ensure that the route matches therobots.txt
path. - Add the following content to the newly created resource route:
const textContent = `User-agent: *Disallow: /dashboard/Allow: /loginAllow: /signupAllow: /$`;export function loader() { return new Response(textContent, { headers: { 'Content-Type': 'text/plain...