question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom render-html-plugin method

See original GitHub issue

Do you want to request a feature or report a bug? Request a feature

What is the current behaviour? Preact-cli uses its own html-render-plugin and it’s very difficult to extend. For example, in L54 it seems like the prerender method can be customised. But how does one get to pass the configuration for it?

If the current behaviour is a bug, please provide the steps to reproduce.

What is the expected behaviour?

If this is a feature request, what is motivation or use case for changing the behaviour?

I would like to be able to extend render-html-plugin for my own purposes like:

  • Using a css-in-js library that supports SSR
  • Injecting data as initial state etc.

Please mention other relevant information.

I would like to start a discussion on which would be the best way to accomplish this. Would a PR that aims to do the following look good?

  • extract render-html-plugin as a separate package in the preact-cli monorepo (and use this plugin as default at this stage)
  • as a next step, make render-html-plugin configurable by initialising it in preact.config.js. If not initialised then behaviour can be using default settings maybe
  • alternative plugins can also be initialised in preact.config.js, in which case render-html-plugin is not used

Please paste the results of preact info here.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
kenny-fcommented, Nov 16, 2019

I’ve just run into this problem too. It would be nice to have an escape hatch for customising the prerender.

I managed to find a solution after reading the preact-cli source code and coming up with an elaborate workaround that doesn’t involve forking the repo.

I’ll detail the workaround here for other people running into the same issues. The problem I was trying to solve was that there was a FOUC when using material-ui because their styles were not being inlined, but this could easily be the case for styled-components, emotion etc.

// index.js - export a function to use later
import { ServerStyleSheets } from '@material-ui/core/styles';
import renderToString from 'preact-render-to-string';

export const createCss = () => {
	const sheets = new ServerStyleSheets();

	renderToString(
		sheets.collect(<App />)
	);

	return sheets.toString();
};

create a plugin by creating a preact.config.js file so that we can add a function to the HtmlWebpackPlugin

const { resolve } = require('path');

const createServerStyleSheets = (env) => () => {
	if (!env.ssr) {
		let entry = resolve(env.dest, './ssr-build/ssr-bundle.js');
    
		return require(entry).createCss();
	}
};

export default (config, env, helpers) => {
	const createCss = createServerStyleSheets(env);

	const plugins = helpers.getPluginsByName(config, 'HtmlWebpackPlugin');
	plugins.forEach((p) => {
		p.plugin.options.css = createCss;
	});
};

Create a template.html which is a copy of the default preact-cli one except with the extra line of injected inline styles where we call the function we attached in the custom plugin.

    <style id="jss-server-side"><%= htmlWebpackPlugin.options.css() %></style>

This took me a while to figure out. It would be great if there was a way to do this with a config option.

0reactions
developitcommented, Apr 2, 2020

I created a solution for styled-components that adds support for stylesheet SSR in preact-cli, and doesn’t really require any special configuration or tweaks (aside from defining an SSR constant that we should probably have added in core anyway).

You can check it out here: https://gist.github.com/developit/23fc4995c3b2c07bd8ff3f916565bb03

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the frontend rendering - Django-fluent-contents
The plugin class renders the model instance using: A custom render() method. The render_template attribute, get_render_template() method and optionally ...
Read more >
Sample code for rendering custom HTML - IBM
The sample custom renderer class accesses the data that was retrieved by the data accessor class, processes it, and then creates HTML to...
Read more >
Render Custom Content - Visual Composer API
This helper element is used to display the original element in the editor while storing the raw content, both shortcode and HTML string...
Read more >
A Guide to Custom Scully Plugins - DEV Community ‍ ‍
There are several ways to do this, but one way is to write a custom render plugin. This plugin gives you access to...
Read more >
Use a custom render function with a geocoder | Mapbox GL JS
This example uses a custom HTML rendering function to customize the mapbox-gl-geocoder dropdown menu. The mapbox-gl-geocoder plugin enables place search ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found