Skip to content

Commit

Permalink
rename to func
Browse files Browse the repository at this point in the history
  • Loading branch information
rossrobino committed Sep 21, 2024
1 parent e679eed commit 371d3f7
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 107 deletions.
8 changes: 8 additions & 0 deletions .changeset/healthy-bears-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"create-domco": major
"domco": major
---

Renames `+app` to `+func`

If you are using a UI framework with domco it's nice to have an `app` directory that can be imported on the server and client that holds the components of your application. Having the server entry point named `+app` made the function of the module less clear. This change renames the `+app` entry point to `+func`. There are no other breaking changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions apps/docs/src/server/content/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ dist/
│ ├── (_immutable/) - any JS/CSS/immutable assets
│ └── (index.html) - prerendered pages
└── server/
├── app.js - server entry point
├── func.js - server entry point
└── (adapter-entry.js) - if using an adapter
```

## Manual deployment

If you are not using an [adapter](#adapters), you can import `handler` from the `app.js` module and configure your app to use in another environment.
If you are not using an [adapter](#adapters), you can import `handler` from the `func.js` module and configure your func to use in another environment.

The `dist/client/` directory holds client assets. JS and CSS assets with hashed file names will be output to `dist/client/_immutable/`, you can serve this path with immutable cache headers. Other assets like prerendered HTML files are processed and included in `dist/client/` directly.

Check out the [deployment examples](/examples#deployment) to see how to do this.

## Adapters

Add a deployment adapter within your Vite config to output your app to a different target with no additional configuration.
Add a deployment adapter within your Vite config to output your application to a different target with no additional configuration.

### Example

Expand All @@ -51,7 +51,7 @@ export default defineConfig({

### Cloudflare

The [Cloudflare](https://cloudflare.com) adapter outputs your app to run on [Cloudflare Pages](https://pages.cloudflare.com/).
The [Cloudflare](https://cloudflare.com) adapter outputs your application to run on [Cloudflare Pages](https://pages.cloudflare.com/).

- Function runs on [workerd](https://github.com/cloudflare/workerd).
- Outputs public assets to be served on Cloudflare's CDN.
Expand All @@ -60,7 +60,7 @@ The [Cloudflare](https://cloudflare.com) adapter outputs your app to run on [Clo

### Deno

The [Deno](https://deno.com) adapter outputs your app to run on [Deno Deploy](https://deno.com/deploy). You do not have to use Deno to build your app to use this adapter.
The [Deno](https://deno.com) adapter outputs your application to run on [Deno Deploy](https://deno.com/deploy). You do not have to use Deno to build your func to use this adapter.

- Function runs on Deno.
- Static files are served with [`@std/http/file-server`](https://jsr.io/@std/http).
Expand All @@ -69,7 +69,7 @@ The [Deno](https://deno.com) adapter outputs your app to run on [Deno Deploy](ht

### Vercel

The [Vercel](https://vercel.com) adapter outputs your app to the [Build Output API](https://vercel.com/docs/build-output-api/v3) specification.
The [Vercel](https://vercel.com) adapter outputs your application to the [Build Output API](https://vercel.com/docs/build-output-api/v3) specification.

- Function runs on [Node.js](https://vercel.com/docs/functions/runtimes#node.js), [Node.js with ISR](https://vercel.com/docs/incremental-static-regeneration), or [Edge Runtime](https://vercel.com/docs/functions/runtimes/edge-runtime).
- Outputs public assets to be served on Vercel's [Edge Network](https://vercel.com/docs/edge-network/overview).
Expand All @@ -79,14 +79,14 @@ The [Vercel](https://vercel.com) adapter outputs your app to the [Build Output A

### Creating an adapter

If you'd like to deploy a domco app to a different provider, and you need many configuration steps for this to take place you can create an adapter.
If you'd like to deploy a domco func to a different provider, and you need many configuration steps for this to take place you can create an adapter.

Check out the [current adapters](https://github.com/rossrobino/domco/tree/main/packages/domco/src/adapter) to see how to make your own.

Adapters take care of these deployment steps.

1. Set [`ssr`](https://vitejs.dev/config/ssr-options.html) options if changes are needed, for example if you are deploying to an edge function, set this to `"web worker"`.
2. Create an entry point for the target environment by using the `handler` from `dist/server/app.js`. This could be reexporting it as a different export, or applying to a node server.
2. Create an entry point for the target environment by using the `handler` from `dist/server/func.js`. This could be reexporting it as a different export, or applying to a node server.
3. Within the entry point, serve the `dist/client/*` directory as static assets, and the `dist/client/_immutable/*` directory with immutable cache headers. Static assets must be hit before the `handler` to take advantage of prerendering. When an asset is not found, the request needs to fallthrough to the `handler`.

If you think others might benefit from your adapter you can [create an issue](https://github.com/rossrobino/domco/issues) or pull request to propose a new adapter. Adapters should be created for zero configuration deployments for deployment providers, not specific to JavaScript runtimes.
9 changes: 5 additions & 4 deletions apps/docs/src/server/content/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Here are some examples of how to use a few popular server-side frameworks with d
[Hono](https://hono.dev/) is a fast, lightweight server framework built on Web Standards with support for any JavaScript runtime.

```ts
// src/server/+app.ts
// src/server/+func.ts
import { html } from "client:page";
import { Hono } from "hono";

Expand All @@ -27,7 +27,7 @@ export const handler = app.fetch;
[h3](https://h3.unjs.io/) is a server framework built for high performance and portability running in any JavaScript runtime.

```ts
// src/server/+app.ts
// src/server/+func.ts
import { html } from "client:page";
import { createApp, eventHandler, toWebHandler } from "h3";

Expand All @@ -47,6 +47,7 @@ If you just want to add a router, and create your own context for each route, he
[Trouter](https://github.com/lukeed/trouter) is a fast, small-but-mighty, familiar ~~fish~~ router.

```ts
// src/server/+func.ts
import { html } from "client:page";
import type { Handler } from "domco";
import { Trouter, type Methods } from "trouter";
Expand Down Expand Up @@ -103,12 +104,12 @@ export const handler: Handler = async (req) => {

### Node server

Here's an example of how to serve your app using the result of your build using `node:http`, [`sirv`](https://github.com/lukeed/sirv/tree/master/packages/sirv), and [`domco/listener`](https://github.com/rossrobino/domco/blob/main/packages/domco/src/listener/index.ts).
Here's an example of how to serve your func using the result of your build using `node:http`, [`sirv`](https://github.com/lukeed/sirv/tree/master/packages/sirv), and [`domco/listener`](https://github.com/rossrobino/domco/blob/main/packages/domco/src/listener/index.ts).

```ts
// server.js
// import the `handler` from the build output
import { handler } from "./dist/server/app.js";
import { handler } from "./dist/server/func.js";
// converts web handler to a Node compatible request listener
import { nodeListener } from "domco/listener";
import { createServer } from "node:http";
Expand Down
66 changes: 33 additions & 33 deletions apps/docs/src/server/content/generated/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ A function that returns an additional entry point to include in the SSR build.

**AdapterEntryOptions**

**AdapterEntryOptions.appId**: `string`
**AdapterEntryOptions.funcId**: `string`

The app entry point to import `handler` from.
The function entry point to import `handler` from.

#### Returns

Expand Down Expand Up @@ -169,34 +169,6 @@ Middleware used in the Vite server for dev and preview.

---

<a id="appmodule" name="appmodule"></a>

### AppModule

> **AppModule**: `object`
Exports from the SSR `app` entry point.

#### Type declaration

<a id="handler" name="handler"></a>

##### handler

> **handler**: [`Handler`](globals.md#handler-1)
<a id="prerender" name="prerender"></a>

##### prerender

> **prerender**: [`Prerender`](globals.md#prerender-1)
#### Defined in

[types/index.ts:7](https://github.com/rossrobino/domco/blob/main/packages/domco/src/types/index.ts#L7)

---

<a id="domcoconfig" name="domcoconfig"></a>

### DomcoConfig
Expand All @@ -218,7 +190,7 @@ Pass the config into the `domco` vite plugin.
domco adapter.

Defaults to `undefined` - creates a `app` build only.
Defaults to `undefined` - creates a `func` build only.

###### Default

Expand Down Expand Up @@ -254,6 +226,34 @@ export default defineConfig({

---

<a id="funcmodule" name="funcmodule"></a>

### FuncModule

> **FuncModule**: `object`
Exports from the SSR `+func` entry point.

#### Type declaration

<a id="handler" name="handler"></a>

##### handler

> **handler**: [`Handler`](globals.md#handler-1)
<a id="prerender" name="prerender"></a>

##### prerender

> **prerender**: [`Prerender`](globals.md#prerender-1)
#### Defined in

[types/index.ts:7](https://github.com/rossrobino/domco/blob/main/packages/domco/src/types/index.ts#L7)

---

<a id="handler-1" name="handler-1"></a>

### Handler()
Expand All @@ -263,7 +263,7 @@ export default defineConfig({
Request handler, takes a web request and returns a web response.

```ts
// src/server/+app.ts
// src/server/+func.ts
import type { Handler } from "domco";

export const handler: Handler = async (req) => {
Expand Down Expand Up @@ -314,7 +314,7 @@ Paths to prerender at build time.
#### Example

```ts
// src/server/+app.ts
// src/server/+func.ts
import type { Prerender } from "domco";

export const prerender: Prerender = ["/", "/post-1", "/post-2"];
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/src/server/content/migrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ hydrateRoot(

## Add a server entry point

Create a server entry `src/server/+app.tsx` file.
Create a server entry `src/server/+func.tsx` file.

```tsx
// /src/server/+app.tsx
// /src/server/+func.tsx
// import your App
import App from "../app/App";
// import the HTML page
Expand Down Expand Up @@ -146,6 +146,6 @@ src/
│ ├── index.css
│ └── main.tsx
├── server/
│ └── +app.tsx
│ └── +func.tsx
└── vite-env.d.ts
```
26 changes: 14 additions & 12 deletions apps/docs/src/server/content/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ deno run -A npm:create-domco@latest

domco identifies the entry points of your application by file name. These entry points are prefixed with `+` so they are easily identifiable.

### +app
### +func

The `app` entry point is located in within `src/server/`, this is the server entry point for your application.
The function entry point is located in within `src/server/`, this is the server entry point for your application.

```txt {3}
src/
└── server/
└── +app.(js,ts,jsx,tsx)
└── +func.(js,ts,jsx,tsx)
```

`+app` modules export a `handler` function that takes in a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), and returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
`+func` modules export a `handler` function that takes in a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), and returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).

```ts
// src/server/+app.ts
// src/server/+func.ts
export const handler = async (req: Request) => {
return new Response("Hello world");
};
Expand All @@ -52,7 +52,7 @@ export const handler = async (req: Request) => {
From here, it's up to you. For example, you could route different requests to different responses, based on the [`req.url`](https://developer.mozilla.org/en-US/docs/Web/API/Request/url).

```ts
// src/server/+app.ts
// src/server/+func.ts
export const handler = async (req: Request) => {
const { pathname } = new URL(req.url);

Expand All @@ -79,7 +79,7 @@ src/
├── client/
│ └── +page.html
└── server/
└── +app.ts
└── +func.ts
```

### +script
Expand All @@ -91,7 +91,7 @@ src/
├── client/
│ └── +script.ts
└── server/
└── +app.ts
└── +func.ts
```

## Virtual Modules
Expand All @@ -104,7 +104,8 @@ domco takes care of these problems using [virtual modules](https://vitejs.dev/gu

You can import the transformed HTML of any `+page.html` from this module or one of it's sub-paths. domco calls Vite's [`transformIndexHtml`](https://vitejs.dev/guide/api-plugin.html#transformindexhtml) hook on the imported page and inlines it into your server build.

```ts {2,8}
```ts {3,9}
// src/server/+func.ts
// returns transformed content of `src/client/+page.html`
import { html } from "client:page";
// `src/client/other/+page.html`
Expand All @@ -126,7 +127,8 @@ You can also easily get the `<script>` tags for any `+script` module on the serv

In development, domco links the scripts to the source. In production, domco [reads](https://vitejs.dev/guide/backend-integration.html) the [manifest](https://vitejs.dev/config/build-options.html#build-manifest) generated by the client build and includes the hashed version of these file names and their imports.

```ts {2,13}
```ts {3,14}
// src/server/+func.ts
// returns transformed content of `src/client/+script.ts`
import { tags } from "client:script";
// `src/client/other/+script.ts`
Expand Down Expand Up @@ -156,11 +158,11 @@ export const handler = async (req: Request) => {
Export a `prerender` variable to prerender routes that respond with HTML.

```ts
// src/server/+app.ts
// src/server/+func.ts
export const prerender = ["/", "/post-1", "/post-2"];
```

After the Vite build is complete, domco will import your production app and request the routes provided. The responses will be written into `dist/client/(prerender-path)/index.html` files upon build.
After the Vite build is complete, domco will import your function and request the routes provided. The responses will be written into `dist/client/(prerender-path)/index.html` files upon build.

If you are using an [adapter](/deploy#adapters), these static files will be served in front of your request handler. So when an `index.html` file is found for the route, it is served directly without hitting your handler.

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions packages/create-domco/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import app from "./template-files/app.js";
import denoJson from "./template-files/deno-json.js";
import envTypes from "./template-files/env-types.js";
import favicon from "./template-files/favicon.js";
import func from "./template-files/func.js";
import gitignore from "./template-files/gitignore.js";
import packageJson from "./template-files/package-json.js";
import pageHtml from "./template-files/page-html.js";
Expand Down Expand Up @@ -111,7 +111,7 @@ export const createDomco = async () => {

const getAllTemplateFiles: GetTemplateFile = (options) => {
const getTemplateFileFunctions = [
app,
func,
denoJson,
favicon,
gitignore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const getTemplateFiles: GetTemplateFile = ({ lang }) => {
const isTs = lang === "ts";
return [
{
name: `src/server/+app.${lang}`,
name: `src/server/+func.${lang}`,
contents: `${isTs ? `import type { Handler } from "domco";` : `/** @import { Handler } from "domco" */`}
import { html } from "client:page";
${isTs ? "" : "\n/** @type {Handler} */"}
Expand Down
4 changes: 2 additions & 2 deletions packages/domco/src/adapter/cloudflare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export const adapter: AdapterBuilder = async () => {
message:
"created Cloudflare Pages build .cloudflare/\ninstall wrangler and run `wrangler pages dev .cloudflare` to preview your build\nhttps://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler#installation\nhttps://developers.cloudflare.com/workers/wrangler/commands/#pages",

entry: ({ appId }) => {
entry: ({ funcId }) => {
return {
id: "_worker",
code: `
import { handler } from "${appId}";
import { handler } from "${funcId}";
export default { fetch: handler };
`,
Expand Down
4 changes: 2 additions & 2 deletions packages/domco/src/adapter/deno/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const adapter: AdapterBuilder = async () => {
message:
"created Deno build dist/\n\nrun `deno run -A dist/server/main.js` to start the server.",

entry: ({ appId }) => {
entry: ({ funcId }) => {
return {
id: "main",

Expand All @@ -41,7 +41,7 @@ export const adapter: AdapterBuilder = async () => {
* falling back to the handler.
*/
code: `
import { handler } from "${appId}";
import { handler } from "${funcId}";
import { serveDir } from "https://jsr.io/@std/http/1.0.6/file_server.ts";
const getStatic = async (req) => {
Expand Down
Loading

0 comments on commit 371d3f7

Please sign in to comment.