Coder Social home page Coder Social logo

SSG + Client script about honox HOT 5 CLOSED

berlysia avatar berlysia commented on May 18, 2024 1
SSG + Client script

from honox.

Comments (5)

berlysia avatar berlysia commented on May 18, 2024 1

Great. I will try it.

from honox.

berlysia avatar berlysia commented on May 18, 2024

(edited) I thought "it works!" but there is errors. I folded up what I was writing.

References which points to generated codes built by client build cause not found and fallback to index.html

And previous workarounds 1 and 2 are also have same problem, but there is no error because the files by client build is also in dist directory.

image

(folded)

(update) Just work with this configuration:

// vite.config.ts
import honox from "honox/vite";
import client from "honox/vite/client";
import { defineConfig } from "vite";
import ssg from "@hono/vite-ssg";

const entry = "./app/server.ts";

export default defineConfig(({ mode, command }) => {
  if (mode === "client") {
    return {
      plugins: [client()],
      build: {
        outDir: ".hono",
      },
    };
  }

  return {
    plugins: [honox(), ssg({ entry })],
  };
});

And fix manifest position for Script component.

// app/routes/_renderer.tsx
import { Style } from "hono/css";
import { jsxRenderer } from "hono/jsx-renderer";
import { Script } from "honox/server";
import { Manifest } from "vite";

// here
const manifestFile = Object.values(
  import.meta.glob<{ default: Manifest }>("/.hono/.vite/manifest.json", {
    eager: true,
  }),
)[0];
const manifest = manifestFile.default;

export default jsxRenderer(({ children, title }) => {
  return (
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>{title}</title>
        <Script src="/app/client.ts" manifest={manifest} />
        <Style />
      </head>
      <body>{children}</body>
    </html>
  );
});

from honox.

yusukebe avatar yusukebe commented on May 18, 2024

@berlysia

Thanks! I'll check the details later!

from honox.

berlysia avatar berlysia commented on May 18, 2024

Hi, looks like it's working this time, I believe.

in short: Just work with build client and toSSG into dist. For my usecase, @hono/vite-ssg is useless.

Sample repository is here (and deployed here).

◆If you have no time, you can skip to after <hr /> 😉◆

In my investigation, following config achieves the subject.

vite.config.ts
// vite.config.ts
import ssg from "@hono/vite-ssg";
import honox from "honox/vite";
import client from "honox/vite/client";
import { defineConfig } from "vite";
import devServer from "@hono/vite-dev-server";

const entry = "app/server.ts";

export default defineConfig(({ mode }) => {
  if (mode === "client") {
    // Client build
    return {
      plugins: [client()],
      build: {
        outDir: ".hono", // <--
        emptyOutDir: true,
      },
    };
  }

  {
    // SSG build
    return {
      plugins: [
        honox(),
        devServer({ entry }),
        ssg({ entry }),
      ],
    };
  }
});
build log
$ bun run build
$ vite build --mode client && vite build
vite v5.1.3 building for client...
✓ 23 modules transformed.
.hono/.vite/manifest.json         0.37 kB │ gzip: 0.19 kB
.hono/static/counter-CDwTLWFe.js  0.22 kB │ gzip: 0.20 kB
.hono/static/client-DnYkLGI4.js   6.54 kB │ gzip: 3.09 kB
✓ built in 99ms
GET  /
Build files into temp directory: .hono
Generated: .hono/index.html
vite v5.1.3 building for production...
✓ 6 modules transformed.
../dist/index.html                           0.51 kB │ gzip: 0.36 kB
../dist/assets/counter-CDwTLWFe-B0y06f1g.js  0.22 kB │ gzip: 0.20 kB
../dist/assets/index-D-Zp7aOz.js             8.22 kB │ gzip: 3.58 kB
✓ built in 63ms

Build takes following steps:

  1. vite build --mode client.
    • Clean up .hono directory and emit client bundles and manifest.json .
  2. vite build.
    1. @hono/vite-ssg generates pages by toSSG into .hono directory.
      • The pages have reference to the client bundles, thanks to manifest.json .
    2. Vite builds with pages generated in the former step in .hono directory.
    3. Emit final bundles and pages into dist directory.

toSSG -> vite build generates __vite__mapDeps.viteFileDeps twice . But there is no error because __vite__mapDeps.viteFileDeps is empty.
This is remote cause the weird errors described in #48 (comment) .

image


But, I think we don't need the step 2-ii, 2-iii . Just work with build client and toSSG into dist, right?
This fundamentally resolves the problem surrounding __vite__mapDeps.viteFileDeps .

So what I want is just like this:

  1. vite build with plugins: [client()]
  2. Run build.ts that contains https://github.com/honojs/vite-plugins/blob/55e3abfb8017c7ee9606458da665e3f09e0d428f/packages/ssg/src/ssg.ts#L34-L50 , with NODE_ENV=production

from honox.

yusukebe avatar yusukebe commented on May 18, 2024

Hi @berlysia

Sorry to be late!

But, I think we don't need the step 2-ii, 2-iii . Just work with build client and toSSG into dist, right?

Indeed, you are right. I've looked into your sample repository, that's simple and great! Perhaps, we can apply that way to the @hono/vite-ssg. Can you try to fix the @hono/vite-ssg to support it?

from honox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.