platform
This would add inline/external for components to fetch data, and be exposed to the component through an injectable function.
Requirements
Inline
// page.component.ts
import { AsyncPipe, JsonPipe } from "@angular/common";
import { Component } from "@angular/core";
import db from './db';
import { useLoader } from "@analogjs/router";
export const loader = async() => {
const items = db.getItems();
return {
items: items
};
};
@Component({
selector: 'app-test',
standalone: true,
imports: [AsyncPipe, JsonPipe],
template: `
Test works
{{ data$ | async | json }}
`
})
export default class TestComponent {
data$ = useLoader<typeof loader>(); // <-- { items: Item[] }
}
External
// page.server.ts
import db from './db';
export const loader = async() => {
const items = db.getItems();
return {
items: items
};
};
// page.component.ts
import { AsyncPipe, JsonPipe } from "@angular/common";
import { Component } from "@angular/core";
import { useLoader } from "@analogjs/router";
@Component({
selector: 'app-test',
standalone: true,
imports: [AsyncPipe, JsonPipe],
template: `
Test works
{{ data$ | async | json }}
`
})
export default class TestComponent {
data$ = useLoader<typeof loader>(); // <-- { items: Item[] }
}
No response
Dependency injection works as normal on the client side, as it's a resolver that calls a backend API. The useLoader
is a wrapper around the ActivatedRoute
and a resolve property. Nothing too complex there to start. Loaders on route components feel too invasive and require going outside the normal component path
Dependency injection works as normal on the client side, as it's a resolver that calls a backend API. The
useLoader
is a wrapper around theActivatedRoute
and a resolve property. Nothing too complex there to start. Loaders on route components feel too invasive and require going outside the normal component path
I'm referring to inversion of control/dependency injection on the server side :)
Ahh, no its not doing that. I don't see much value in having DI there as you would mostly be using imports to access server-only code like Prisma, file-system access, etc.
I don't necessarily agree with that statement. I've used Remix extensively, to build a subscription-based content creation platform, and it's a pain in the ass without DI on the server side. The amount of boilerplate code and workarounds required is crazy. Angular already supports DI for SSR, so I don't see why loaders shouldn't.
I didn't even know that until you pointed it out!
Seems like the tree shaken loader that you describe would be perfect then.
I need to try it, but I assume that Nitro automatically serves a pre-rendered route and if no static html exists it generates the page on each request. Then, having two functions definitely seems unnecessary.
I don't want to hijack this RFC with (sort of) unrelated things but looking at this NextJS documentation and this configuration option for Nitro made me think that it would be cool if you could add some config snippet (like 'use static';
or 'use swc';
) to the top of the file and based on that the route would be marked as static, cached, etc.
Owner Name | analogjs |
Repo Name | analog |
Full Name | analogjs/analog |
Language | TypeScript |
Created Date | 2022-07-06 |
Updated Date | 2023-03-28 |
Star Count | 885 |
Watcher Count | 18 |
Fork Count | 67 |
Issue Count | 33 |
Issue Title | Created Date | Updated Date |
---|