Replies: 4 comments 10 replies
-
|
the way i thought it would work is by exporting a const with data: e.g. let saved_data: MyData;
#server {
async function myFunc(): MyData { /* do async stuff to get data */ }
if (!saved_data) {
saved_data = await myFunc();
}
export {
data: saved_data,
}
}and then you can use this or multiple of these in the components via component A() {
let data = #server.data;
... use it any way you like
}and I assumed since the |
Beta Was this translation helpful? Give feedback.
-
|
I think the first option would definitely confuse users who will want to name their functions I like the last option the best, and I think it makes perfect sense that if several components are declared in the same file as a #server block, they all get access to that block. I assume it also lets the user define a named function so they can both return it here and also reference it on client side: #server {
async function doSomething(){
return { ... };
}
return doSomething;
}
export component Foo(props, data) {
// data is available here
// but so is #server.doSomething() in case the user needs to update the data?
} |
Beta Was this translation helpful? Give feedback.
-
|
Maybe it's as simple as constraining it to be this then? #server {
import { load } from 'ripple/server';
const message = await load(({ params }) => "Hello from server");
export {
message
};
}
export component Foo(prop) {
let data = #server.message;
}The constraint is that any top-level |
Beta Was this translation helpful? Give feedback.
-
|
One question, what would be the recommended way to handle something like database connection pooling, as we woildn't want this to happen on every request. // What you'd WANT: run once at startup, reuse pool forever
#server(context) {
import { Pool } from 'pg';
// This would run on EVERY request; completely wrong for pooling
const pool = new Pool({ connectionString: process.env.DB_URL });
const user = await pool.query('SELECT * FROM users WHERE id = $1', [context.params.id]);
export { user };
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
A common pattern is to load data once on the server, what if we had this API that enabled it?
The user would not be permitted to reference
loadin the client side code, it's purely server only.Alternatively, it could be something like this? Where you return in the server block at the top level to populate the data
The second one looks appealing but then we can't pass in things like
params.Then it got me thinking, it could be this?
The other confusing part is that there can be multiple components in a module, so do they all get access to this data?
Beta Was this translation helpful? Give feedback.
All reactions