Single File Page. (Being able to define everything in a single file `.page.js`.)
See original GitHub issueInstead of having several page files (.page.js
, .page.route.js
, .page.server.js
, .page.client.js
), we would define everything in .page.js
(while vite-plugin-ssr
automatically statically extracts the relevant parts like in https://next-code-elimination.vercel.app.)
Many people have expressed a longing for this, but I’m on the fence. Simply because I highly value clarity and simplicity: it’s obvious and simple what the .page.js
, .page.route.js
, .page.server.js
, and .page.client.js
files are about, whereas if we merge everything in .page.js
it becomes less clear what is run in what environment.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:77 (66 by maintainers)
Top Results From Across the Web
One JS File for Multiple Pages [closed] - Stack Overflow
The answer for this is surely 'depends on the situation' (we know that). My question is, assuming all my JS logic is placed...
Read more >Use JavaScript within a webpage - Learn web development
Take your webpages to the next level by harnessing JavaScript. Learn in this article how to trigger JavaScript right from your HTML documents....
Read more >How to package everything into single HTML file? · Issue #1704
I have a fairly simple site, it is a single page React app. When I do a build of the site I end...
Read more >Single-page application vs. multiple-page application - Medium
Its content is loaded by AJAX (Asynchronous JavaScript and XML) — a method of exchanging data and updating in the application without refreshing...
Read more >Single-File Components - Vue.js
Vue Single-File Components (a.k.a. *.vue files, abbreviated as SFC) is a special file ... Single-Page Applications (SPA); Static Site Generation (SSG) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Update:
_define.ts
can be a plain normal JavaScript file.The trick is to set hook properties to a string that represent the path to the hook file:
That way, we can skip this whole thing of
_define.js
not being real JavaScript.It’s now real JavaScript, it just happens to not import any code. (We can show a warning if
_define.js
imports code because this shouldn’t be needed.)Buckle up, this is a long one.
I think we’re definitely moving in the right direction here. 👍
The last part is my favorite. You’re kind of starting to move in the direction of my “perfect” framework. Some sort of single export that “automagically” sets the configuration for the page, but doesn’t prevent the user from going deeper if they want. All that’s really happening is sensible defaults are being set.
That really aligns with my personal philosophy about simplifying configuration: Zero-config doesn’t mean no-config, it means optional-config. It just means sensible defaults. The config is just for when you need to override those defaults.
So if you’ll bear with me, I’m going to go wild and just describe my “perfect” config scenario for VPS. With complete imaginary freedom, for me it looks something like this:
Context Object
Some Discussion Points
Why didn’t you use
SSR
,SPA
andHTML
for the modes?Those terms are commonly used to describe general approaches to rendering a web application, and there also seems to be no standard at all for them. Some people use the termCSR
instead ofSPA
. Technically all pages useHTML
. At first impression, “server-side rendering” sounds like exactly what PHP or any other regular server with a templating engine does. But here in VPS, we’re always trying to render a page using a JS framework that can render on both the client and the server, so the key question is: Do we want to render it on the server, the client, or both with hydration? I vote we throw the arcane acronyms out the window entirely (except in the docs, for clarity, of course).I’ve changed my mind on this point. I’m probably going to start a separate discussion where we can go deeper into my thoughts on this particular issue. I think those acronyms have their place, but they’re often used to mean the wrong thing. Here are some articles I’ve been reading that I want to use as a basis to hash out a “standard” for VPS to potentially use.
https://docs.astro.build/en/concepts/mpa-vs-spa/
https://dev.to/snickdx/understanding-rendering-in-web-apps-spa-vs-mpa-49ef
Why no
@server
or@server-only
or whatever comments?I wasn’t in love with this style to begin with. Relying on comments to do actual logic just seems like a bad idea from the start, in my humble opinion. Using the config above, they’re no longer necessary.
What about
import
statements?Instead of using comments to control the target environment for
import
statements, I propose a more “Vite-ish” style by using query parameters in the URL string. So likeimport "/styles/index.css?env=[server|client]";
to scope the import. Without the parameter it is by default in the client for theserver-and-client
andclient-only
modes, and by default in the server forserver-only
mode. I don’t know if that would work, though? What are your thoughts?Didn’t you basically just describe a whole new plugin?
Kind of. I did say I was going to go wild. 😅
Why such an extreme change?
VPS’s way of looking at page exports is super cool, but it clutters the page with a bunch of exports and has the adverse effect of unescapable potential naming conflicts and verbose export names. Putting the entire config under a single export is so much cleaner IMO. Also it ensure VPS won’t conflict with any future framework… I think?
Why did you rename everything??
I just felt like most of the names are more verbose than necessary. Don’t hate me! I still think the current VPS is absolutely amazing and I totally love it.