1

New to Java so appologies if this is a dumb question> I am trying to deploy my webpage on Vercel and it is building sucessfully however whenever I visit the webpage I get

Uncaught SyntaxError: Unexpected token '<'

I belive this is because my bundle.js is returning html when it shouldnt be. When I deploy the site locally with npm build or npx serve it works fine and everything is where it should be. Any advice would be appriciated as I am completly lost at this point. The site is currently hosted at https://test-seven-mauve-23.vercel.app/

To recreate the issue, you can go to the vercel link above, inspect the page and you will see the syntax error in the console. To check bundle.[hash].js you can click network and refrsh the page. You can see the bundle[hash].js contains HTML code under responce.

This is rollup.config.js

import fs from 'fs';
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import posthtml from 'posthtml';
import { hash } from 'posthtml-hash';
import rimraf from 'rimraf';
import copy from 'rollup-plugin-copy';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;
    
    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

function hashAssets() {
    return {
        name: 'hash-assets',
        buildStart() {
            rimraf.sync('build');
        },
        writeBundle() {
            posthtml([
                hash({ path: 'build/' }),
            ])
            .process(fs.readFileSync('./build/index.html'))
            .then((result) => fs.writeFileSync('./build/index.html', result.html));
        }
    }
}

export default {
    input: 'src/main.ts',
    output: {
        sourcemap: !production,
        format: 'iife',
        name: 'app',
        file: 'build/bundle.[hash].js'
    },
    plugins: [
        copy({
            targets: [{
                src: 'public/*',
                dest: 'build/',
            }],
        }),
        svelte({
            // enable run-time checks when not in production
            dev: !production,
            // we'll extract any component CSS out into
            // a separate file - better for performance
            css: css => {
                css.write('bundle.[hash].css', !production);
            },
            preprocess: sveltePreprocess(),
        }),

        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),
        typescript({
            sourceMap: !production,
            inlineSources: !production
        }),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('build'),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser(),

        production && hashAssets()
    ],
    watch: {
        clearScreen: false
    }
};

This is index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">

    <title>SWGoH Status</title>

    <link rel="icon" type="image/png" href="/favicon_chewie.png">
    <link rel="stylesheet" href="/build/bundle.[hash].css">

    <script defer="" src="/build/bundle.[hash].js"></script>
</head>

<body>
</body>
</html>


This is main.ts

import App from './App.svelte'

const app = new App({
    target: document.body,
})

export default app

I feel like I have tried everything but probably not,

I tried toubleshooting with npx serve, I tried another host, I tried absolute paths.

I'm expecting bundle[hash] not to return HTML

4
  • 1
    "I belive this is because my bundle.js is returning html when it shouldnt be" That sounds about right. Did you take a look at the actual response returned? Chances are that it's an error page which may even include some useful information. Commented Mar 27, 2024 at 16:34
  • Heya thanks for the comment, yea the only error the Web console gives me is the unexpected token, the actual response returned is just html identical to index.html Commented Mar 27, 2024 at 17:47
  • The policy on this site is no one should have to click on an offsite link to reproduce the problem. Commented Mar 27, 2024 at 22:50
  • Apologies, I was unaware. Ok I Will update the post and investigate how I can reproduce the issue while including the minimum amount of code possible Commented Mar 27, 2024 at 23:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.