Handling Assets and Static Files in Vite Quiz Quiz

Delve into key concepts for managing assets and static files in Vite, including import methods, directory usage, deployment implications, and referencing strategies. Enhance your understanding of modern asset handling best practices within fast build tools using this focused assessment.

  1. Importing Images Directly

    When you import an image as a module in your JavaScript file (e.g., import logo from './logo.png'), what does the imported value represent during production builds?

    1. A base64 encoded string regardless of size
    2. The original relative file path written in code
    3. The resolved public URL of the optimized image
    4. The raw binary contents of the image

    Explanation: The correct answer is 'The resolved public URL of the optimized image' because importing an image via the module system in modern build tools resolves to a final, deployable URL which points to the optimized asset. The raw binary contents aren't returned since modules don't import files as raw data. Base64 encoding is used only for very small files, not all images, so that answer is too broad. The import does not return the original file path, as it is replaced with the processed URL during build.

  2. Public Directory Purpose

    Which scenario best illustrates an appropriate use of the 'public' directory when handling assets in a project?

    1. Saving style sheets that are only referenced in Vue or React components
    2. Adding images to be imported by JavaScript using the import statement
    3. Placing a favicon that must be accessible at the root of the deployed site
    4. Storing all JavaScript files for dynamic imports

    Explanation: The public directory is ideal for assets like favicons that should remain at fixed URLs and not undergo processing or hashing. JavaScript files for dynamic imports should stay in the main source or assets directories, not public, to benefit from bundling and optimization. Images imported by JavaScript are better placed in the assets folder, ensuring proper handling during build. Stylesheets referenced internally in components should also remain in the main source tree.

  3. Referencing Static Files in HTML

    How should you reference a static image stored in the 'public' directory from the main HTML file for correct resolution after building?

    1. Use a special placeholder like '%PUBLIC_URL%/my-image.jpg'
    2. Use the path starting with a forward slash, like '/my-image.jpg'
    3. Use the full file system path, like './public/my-image.jpg'
    4. Reference it with a relative path, like './my-image.jpg'

    Explanation: Referencing with a forward slash ensures the asset path is resolved from the deployed site's root, which aligns with how files in the public directory are served. Using the full file system path or a relative path ignores the build output directory structure and can break after deployment. Special placeholders like '%PUBLIC_URL%' are not inherently recognized and should not be relied upon in standard setups.

  4. Static Asset Hashing

    What is the main benefit of automatic file hashing applied to assets in the build output when imported from the assets folder?

    1. It reduces the file size significantly by removing metadata
    2. It converts all files to JSON format for easier processing
    3. It makes assets harder to find for end users, providing security
    4. It enables efficient long-term browser caching and avoids issues with stale assets

    Explanation: Hashing in asset filenames ensures browsers download updated files only when the content changes, improving cache effectiveness. Security through obscurity is not the intention nor a reliable method, so the asset is not hidden for security. Hashes do not reduce file size or strip metadata. Assets are not converted to JSON format; the original format is preserved.

  5. Copying Files to the Output Directory

    During a production build, which files from the 'public' directory are copied to the final output, and how are they treated?

    1. Only files that are imported in JavaScript get included
    2. All static files are bundled and hashed for import as modules
    3. Only image files are copied and optimized automatically
    4. All files and folders are copied as-is without processing or renaming

    Explanation: Everything placed in the public directory is copied over verbatim to the final build output, ensuring their paths and naming remain unchanged. Only image files being copied is incorrect, as the directory can contain any static asset. The files are not bundled, hashed, or processed for import as modules — they are simply made available at their original paths. Assets not referenced in code are still included if present in the public folder.