Coder Social home page Coder Social logo

Svelte stores.js about plenti HOT 4 CLOSED

plentico avatar plentico commented on May 20, 2024
Svelte stores.js

from plenti.

Comments (4)

jimafisk avatar jimafisk commented on May 20, 2024 1

Here's a gif of the example above working once that export statement is adjusted:

counter

from plenti.

jimafisk avatar jimafisk commented on May 20, 2024

Adding the regex for export statements seems to get this working for me locally. Even though it's working functionally in the browser, the build still shows the following errors:

Could not add SSR Component: ReferenceError: writable is not defined
Can't render htmlComponent: TypeError: Cannot read property 'subscribe' of undefined

This is being thrown at the bottom of cmd/build/client.go when passed the following updated ssrStr:

/* generated by Svelte v3.23.2 */
/*import { create_ssr_component } from "svelte/internal";*/

/*import { writable } from "svelte/store";*/
var layout_scripts_stores_svelte = writable(0);

var layout_scripts_stores_svelte_NOT_USED = create_ssr_component(($$result, $$props, $$bindings, $$slots) => {
	return ``;
});

/*export default Component;*/
/*export { layout_scripts_stores_svelte };*/

We need to add the writable() function from node_modules/svelte/store/index.mjs to the v8go ctx. This brings up a larger point about how we're not actually trying to import these libraries at all, we need svelte/store in this case but we could need others. We also only want to import the libraries we need specifically for the current project to avoid bloat.

from plenti.

jimafisk avatar jimafisk commented on May 20, 2024

I was perplexed how the SSR component could fail to be added to the ctx yet still work and be available at public/spa/scripts/stores.js. Then I remembered adding the SSR component to the ctx is only needed to render html. Since this is a script component that doesn't actually create any markup, it's ok to skip this part entirely - the same goes for any script component. The public/spa/scripts/stores.js output is created during the client rendering that happens earlier.

We may be able speed things up marginally (and avoid similar errors) but excluding script only files from the SSR rendering. I don't want to hardcode looking for files in layout/scripts because I want that directory structure to be flexible to the user. A better approach is probably to look for and empty return statement from create_ssr_component() and skip adding it to the ctx if that's the case.

from plenti.

jimafisk avatar jimafisk commented on May 20, 2024

I take it back, we'll still need to add scripts to ctx to keep things like titles working with static html. Also the index page doesn't SSR with the reference to ../scripts/stores.svelte if we don't.

It might be easiest to just add all svelte libraries to the SSR ctx:

  • animate
  • easing
  • internal (* already added)
  • motion
  • store
  • transition

This is only used for SSR-ing to create static HTML so won't add bloat to the created site. Reading the node_module files and loading the libraries will slow the build though.

from plenti.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.