Comment

Comment by simonbw

I think some of this article resonates with me, but I also think a big part of it rubs me the wrong way. It seems to assume that everyone has a webserver running a LAMP stack. If you have a static site on a webserver running PHP then of course PHP is going to require the least amount of effort to make your site mildly dynamic.

On the other hand, if you have nothing, I don't think that the fastest/easiest way to a mildly dynamic website is to use PHP.

I got curious about the most minimalist setup I could get for running a static site that would also easily transition to becoming dynamic piece-by-piece. Here's what I came up with using NextJS:

1. Create a `package.json` containing:

    {
      "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start"
      },
      "dependencies": {
        "react": "^18",
        "react-dom": "^18",
        "next": "14.2.4"
      }
    }
2. Create `app/layout.js` containing

    export default ({ children }) => children;

3. Create your home page at `app/page.js`. This is pretty much just what your index.html would have been before except it's wrapped with `export default () => (/.../)`:

    export default () => (
      <html>
        <head>
          <title>Home</title>
        </head>
        <body>
          <h1>Home</h1>
          <p>Welcome to our website!</p>
        </body>
      </html>
    );

4. Put the rest of your static files in `/public`

Now, when you want to make one of your html files dynamic:

1. Move it from `public/your-page.html` to `app/your-page/page.js`

2. Wrap it with `export default () => (/.../)`

And there you go, you can start using JavaScript and JSX in your page.

Here's a summary of the differences between this approach and creating a PHP-based site:

1. You have to create 2 more files containing 13 more lines of code than you would with PHP

2. You need to find a NextJS web host rather than a PHP web host

3. Your dynamic pages are html-inside-javascript rather than php-inside-html. You are also technically writing JSX and not HTML, which has slightly different syntax.

4. You have a much easier to set up local dev server (Install `node`, then `npm install` and `npm dev`)

5. This framework can scale pretty seamlessly to "highly dynamic", whereas with PHP you'd probably need to introduce a frontend framework if you wanted to create something actually "highly dynamic".

6. You only need to learn one programming language (JavaScript) instead of two (JavaScript and PHP).

parent simonbw 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton