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

Assets are loaded from single spa url instead of microfrontends #13

Open
Savuelo opened this issue Mar 10, 2022 · 7 comments · May be fixed by #15
Open

Assets are loaded from single spa url instead of microfrontends #13

Savuelo opened this issue Mar 10, 2022 · 7 comments · May be fixed by #15

Comments

@Savuelo
Copy link

Savuelo commented Mar 10, 2022

I clone the project and run it without changing anything and microfrontend is unable to pull the assets from correct url.
Microfrontend tries to fetch assets from root-config server instead of his own.

image

@eduardojustos
Copy link

I have the same issue. Opened it in the other repo for single spa and vite (joeldenning/vite-single-spa-root-config#2)

@justsanjit
Copy link

I am facing the same issue. did you guys able to find a fix for this?

@clabnet
Copy link

clabnet commented Apr 11, 2022

+1

@jones1008
Copy link

jones1008 commented Apr 21, 2022

I found a solution:

Set the vite.config.js setting server.origin to the url of the application:

export default {
  ...
  server: {
    origin: 'http://localhost:3000',
  },
}

--> vite reference to server.origin: https://vitejs.dev/config/#server-origin

Additionally you need import the asset via javascript to get it's url. Apparently only then the url gets adjusted from vite automatically:

<script setup>
import logo from "./assets/logo.png"
</script>

<template>
  <img alt="Vue logo" :src="logo" />
  ...
</template>

Edit: I just noticed that for some reason, you have to reinstall the dependencies, so delete the pnpm-lock.yaml file and the node_modules folder and run pnpm install again

jones1008 pushed a commit to jones1008/vite-single-spa-example that referenced this issue Apr 21, 2022
jones1008 pushed a commit to jones1008/vite-single-spa-example that referenced this issue Apr 21, 2022
@jones1008 jones1008 linked a pull request Apr 21, 2022 that will close this issue
@jones1008
Copy link

jones1008 commented Apr 22, 2022

I even found a solution that does not need to import the asset via javascript but makes use of the resolve.alias config in vite:

import { fileURLToPath, URL } from 'url'

export default {
  ...
  server: {
    origin: 'http://localhost:3000',
  },
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
}

You can then import it with the @-syntax directly instead of via javascript and this automatically gets resolved to http://localhost:3000/src/assets/logo.png:

<template>
  <img alt="Vue logo" src="@/assets/logo.png" />
  ...
</template>

A relative path ./assets/logo.png will not work though

jones1008 pushed a commit to jones1008/vite-single-spa-example that referenced this issue Apr 22, 2022
@eduardojustos
Copy link

@jones1008 the problem is that it also happens with bundles that were split. It could be a CSS bundle, an async Vue component that got into another bundle. And informing the URL it is going to be deployed sounds more like a quick fix and anti-pattern than a solution. Don't you think?

@jones1008
Copy link

yes... you may be right...
Maybe that problem won't appear in future Vite releases at all, if they move the prod build to ES modules too. See https://laurentcazanove.com/articles/state-of-vue-2022-amsterdam-recap/#a-note-on-vite-3 and vitejs/vite#8178

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

Successfully merging a pull request may close this issue.

5 participants