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] How to resolved static assets path imported in react components while dev server running #8

Closed
watarutmnh opened this issue Jun 10, 2021 · 6 comments
Labels
question Further information is requested

Comments

@watarutmnh
Copy link

Question

I'm trying out craft-vite with react component.
The assets path which imported in react components are resolved fine in production build, but they aren't while dev server running.
Is there any way I can solve it?

Thanks,

Example

#app.tsx

import React, { useState } from 'react'
import logo from './logo.svg'
import './App.css'

function App() {
  const [count, setCount] = useState(0)

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>Hello Vite + React!!</p>

screenshot 2021-06-10 at 17 01 00

@watarutmnh watarutmnh added the question Further information is requested label Jun 10, 2021
@watarutmnh
Copy link
Author

Additional Info

The image src url 404 error got is https://appexample.test/src/logo.svg
But I can access the asset to https://localhost:3001/src/logo.svg

@khalwat
Copy link
Contributor

khalwat commented Jun 10, 2021

One way is to put the image in the /public dir in the Vite root. https://vitejs.dev/guide/assets.html#the-public-directory

Beyond that I'm not sure -- this is more of a ViteJS question though, so head over here: https://github.com/vitejs/vite/issues

@khalwat khalwat closed this as completed Jun 10, 2021
@khalwat
Copy link
Contributor

khalwat commented Jun 10, 2021

Come to think of it, it could be related to this: vitejs/vite#2196

@watarutmnh
Copy link
Author

@khalwat Thank you sharing the issue thread.
I'm using valet so the @haytor 's comment are very helpful.
I created Local driver and put it on root directory of the project.

LocalValetDriver.php

<?php

class LocalValetDriver extends LaravelValetDriver
{
    /**
     * Determine if the driver serves the request.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return bool
     */
    public function serves($sitePath, $siteName, $uri)
    {
        // The host and port of the Vite dev server.
        $host = 'localhost';
        $port = 3001;

        $timeout = 6;

        // Use fsockopen to ping the server to determine if the driver will handle the request.
        $fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
        return is_resource($fsock) ? true : false;

    }
    /**
     * Determine if the incoming request is for a static file.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string|false
     */
    public function isStaticFile($sitePath, $siteName, $uri)
    {  
        // Application root directory resource loading, resource reference in node_modules
        if (file_exists($staticFilePath = $sitePath . '/' . $uri)
            && is_file($staticFilePath)) {
            return $staticFilePath;
        }
        if (file_exists($staticFilePath = $sitePath . '/web' . $uri)
            && is_file($staticFilePath)) {
            return $staticFilePath;
        }
        return false;
    }
    /**
     * Get the fully resolved path to the application's front controller.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string
     */
    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        return $sitePath . '/web/index.php';
    }
}

It seems to be working ok so far.

@khalwat
Copy link
Contributor

khalwat commented Jul 22, 2021

@watarutmnh
Copy link
Author

@khalwat Good to know that! Thanks!

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

No branches or pull requests

2 participants