Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support assets in a sibling directory #42

Closed
JeanMeche opened this issue Dec 23, 2024 · 6 comments
Closed

Support assets in a sibling directory #42

JeanMeche opened this issue Dec 23, 2024 · 6 comments

Comments

@JeanMeche
Copy link

I was trying to use elysia in combination of Angular SSR.

Here is basically my server config.

import { AngularAppEngine, createRequestHandler } from '@angular/ssr';
import { isMainModule } from '@angular/ssr/node';
import { Elysia } from 'elysia';
import { staticPlugin } from '@elysiajs/static';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

export function app() {
  const server = new Elysia();
  const angularAppEngine = new AngularAppEngine();

  const serverDistFolder = dirname(fileURLToPath(import.meta.url));
  const browserDistFolder = resolve(serverDistFolder, '../browser');

  server.use(staticPlugin({
    prefix: '',
    assets: browserDistFolder,
    alwaysStatic: true,
  }));

  server.get('/*', async (c) => {
    const res = await angularAppEngine.handle(c.request, { server: 'elysia' });
    return res || undefined;
  });

  console.warn('Elysia server started');

  return server;
}

const server = app();
if (isMainModule(import.meta.url)) {
  const port = process.env['PORT'] || 4000;

  server.listen(port, () => {
    console.log(`Elysia server listening on http://localhost:${port}`);
  });
}

export const reqHandler = createRequestHandler(server.fetch);

When building an SSR app, Angular generates 2 bundles, a server and a static csr one:

  • ./server
  • ./browser

The forementioned server config is located in the server directory and as you can see, we need to pull the static asset from the ../browser directory, a sibling of the parent directory.

And it seems like that elysia-static doesn't support that.

@JeanMeche
Copy link
Author

This was fixed by bumping to 1.2 ! 🚀

@draylegend
Copy link

@JeanMeche, thanks for opening the issue! The application is loading properly, but I encountered an error while building the app. The error is:

Error: ENOENT: no such file or directory, scandir '/home/dl/elysia-static-bugs/.angular/prerender-root/browser'

Here’s the full output from the build process:

/home/dl/elysia-static-bugs/node_modules/.bin/nx run app:build

> nx run app:build:production


Error: ENOENT: no such file or directory, scandir '/home/dl/elysia-static-bugs/.angular/prerender-root/browser'
    at async readdir (node:internal/fs/promises:950:18)
    at async xd (file:///home/dl/elysia-static-bugs/.angular/prerender-root/24ad1a33-bc90-456d-a021-f501fd639e6a/server.mjs:316:16758)
    at async Id (file:///home/dl/elysia-static-bugs/.angular/prerender-root/24ad1a33-bc90-456d-a021-f501fd639e6a/server.mjs:316:17397)
    at async _i.drain (file:///home/dl/elysia-static-bugs/.angular/prerender-root/24ad1a33-bc90-456d-a021-f501fd639e6a/server.mjs:8:43953) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'scandir',
  path: '/home/dl/elysia-static-bugs/.angular/prerender-root/browser'
}
Error: ENOENT: no such file or directory, scandir '/home/dl/elysia-static-bugs/.angular/prerender-root/browser'
    at async readdir (node:internal/fs/promises:950:18)
    at async xd (file:///home/dl/elysia-static-bugs/.angular/prerender-root/59adf74f-e832-4d1c-8571-52631a8a516b/server.mjs:316:16758)
    at async Id (file:///home/dl/elysia-static-bugs/.angular/prerender-root/59adf74f-e832-4d1c-8571-52631a8a516b/server.mjs:316:17397)
    at async _i.drain (file:///home/dl/elysia-static-bugs/.angular/prerender-root/59adf74f-e832-4d1c-8571-52631a8a516b/server.mjs:8:43953) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'scandir',
  path: '/home/dl/elysia-static-bugs/.angular/prerender-root/browser'
}
Browser bundles     
Initial chunk files  | Names            |  Raw size | Estimated transfer size
main-GFSKVUMT.js     | main             | 217.62 kB |                60.04 kB
styles-YYJXJO7S.css  | styles           |   4.66 kB |                 1.17 kB

                     | Initial total    | 222.28 kB |                61.21 kB


Server bundles      
Initial chunk files  | Names            |  Raw size
main.server.mjs      | main.server      | 725.71 kB |                        
server.mjs           | server           | 255.82 kB |                        
polyfills.server.mjs | polyfills.server | 237.05 kB |                        
chunk-JAEHAS6C.mjs   | -                |   1.52 kB |                        
chunk-T55IDOPT.mjs   | -                | 976 bytes |                        

Lazy chunk files     | Names            |  Raw size
chunk-PORXTWD6.mjs   | xhr2             |  12.14 kB |                        
chunk-N3SI5I4Q.mjs   | xhr2             |  12.07 kB |                        

Prerendered 1 static route.
Application bundle generation complete. [9.428 seconds]

Output location: /home/dl/elysia-static-bugs/dist/apps/app


——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target build for project app (10s)


Process finished with exit code 0

It seems the build is expecting a browser directory in the .angular/prerender-root path, which is missing.

Dear devs, let me know if you'd like me to open a new issue for this.

Repo

@JeanMeche
Copy link
Author

@draylegend I couldn't reproduce the issue. Could you try to delete .angular and retry ?

@draylegend
Copy link

@JeanMeche I'm using WSL2 with debian 12 and bun 1.1.42. I deleted .angular and .nx folders, but still builds with the same error

@JeanMeche
Copy link
Author

JeanMeche commented Dec 24, 2024

I have too, I just missed it as it doesn't break the build.

It might be worth reporting to the angular/cli repo directly.

@draylegend
Copy link

draylegend commented Dec 24, 2024

Reported in angular/angular-cli#29227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants