Bhaumin
Bhaumin4w ago

Handle routes for multiple domains using Fresh and Deno

How should we maintain multiple routes of multiple sub domains using fresh and deno? As per documentation we need to add all our routes inside routes folder. but what if we want to maintain routes per domain per feature? Is it possible?
6 Replies
Bhaumin
Bhaumin4w ago
@marvinh. Can you please help us here?
marvinh.
marvinh.4w ago
Fresh's routing is solely based on path names. It doesn't take subdomains into account I'm not aware of a workaround for that
Bhaumin
Bhaumin4w ago
ok thanks for clarity can you help us to define routes per subdomain for folder structure like below
routes/
index.tsx
_middleware.ts // Subdomain routing middleware
src/
domain1/
routes/
_middleware.ts
index.tsx
otherRoutes.tsx // Additional routes for domain1
_layout.tsx
domain2/
routes/
_middleware.ts
index.tsx
otherRoutes.tsx // Additional routes for domain2
_layout.tsx
routes/
index.tsx
_middleware.ts // Subdomain routing middleware
src/
domain1/
routes/
_middleware.ts
index.tsx
otherRoutes.tsx // Additional routes for domain1
_layout.tsx
domain2/
routes/
_middleware.ts
index.tsx
otherRoutes.tsx // Additional routes for domain2
_layout.tsx
we tried to find how to set routes folders for sub domains like above in fresh but didnt get anything.
marvinh.
marvinh.4w ago
I'd treat each domain as a separate Fresh app and have the top level router route to them
Bhaumin
Bhaumin4w ago
can you help me to define top level router to route to them? and how deno project should connect? we want to have only one deno project for this
marvinh.
marvinh.4w ago
Probably something like this:
import { createHandler } from "$fresh/server.ts";

import manifest1 from "./src/domain1/fresh.gen.ts";
import config1 from "./src/domain1/fresh.config.ts";

import manifest2 from "./src/domain2/fresh.gen.ts";
import config2 from "./src/domain2/fresh.config.ts";

const handler1 = createHandler(manifest1, config1);
const handler2 = createHandler(manifest2, config2);

Deno.serve((req, info) => {
const url = new URL(req.url);
if (url.hostname === "domain1") {
return handler1(req, info)
} else if (url.hostname === "domain2") {
return handler2(req, info)
}

return new Response("Not Found", { status: 404 });
});
import { createHandler } from "$fresh/server.ts";

import manifest1 from "./src/domain1/fresh.gen.ts";
import config1 from "./src/domain1/fresh.config.ts";

import manifest2 from "./src/domain2/fresh.gen.ts";
import config2 from "./src/domain2/fresh.config.ts";

const handler1 = createHandler(manifest1, config1);
const handler2 = createHandler(manifest2, config2);

Deno.serve((req, info) => {
const url = new URL(req.url);
if (url.hostname === "domain1") {
return handler1(req, info)
} else if (url.hostname === "domain2") {
return handler2(req, info)
}

return new Response("Not Found", { status: 404 });
});