loadable-components icon indicating copy to clipboard operation
loadable-components copied to clipboard

Content Hash

Open radha-bacancy opened this issue 6 years ago • 0 comments

Hey,

I've added loadable components to my react SSR application.

This is my extractor code

import ReactDOMServer from 'react-dom/server';
import {ChunkExtractor} from '@loadable/server';
import {v4 as uuidv4} from 'uuid';
import {renderStaticOptimized} from 'glamor/server';
import {attrs} from './html';
const path = require('path');

const statsFile = path.resolve('./build/loadable-stats.json');

const extractor = new ChunkExtractor({
  statsFile,
  entrypoints: ['server'],
  outputPath: path.resolve('./out'),
  publicPath: process.env.NODE_ENV === 'production' ? process.env.WEBPACK_PUBLIC_PATH : ''
});

export const renderToString = ({
  element,
  componentName,
  provider,
  props,
  existingDomId,
  htmlOptions,
  uuid = uuidv4(),
  stateId = null
}) => {
  const {html, css, ids} = renderStaticOptimized(() =>
    ReactDOMServer.renderToString(extractor.collectChunks(element))
  );
  const glamorIds = JSON.stringify(ids);
  const scriptTags = extractor.getScriptTags();
  const domId = existingDomId ? existingDomId : `${componentName}-react-component-${uuid}`;
  const glamorId = `${componentName}-glamor-${uuid}`;
  return `
  <!-- glamorIds: ${ids.length} -->
  <script async type="application/json" id="${glamorId}">${glamorIds}</script>
  <style>${css}</style>
  <div id="${domId}" data-ssr="true" ${attrs(htmlOptions)}>${html}</div>
  <!-- Loadable Script Tags -->
  ${scriptTags}
  <script>
    renderComponent('${componentName}','${domId}',${
    typeof stateId === 'string' ? `'${stateId}'` : stateId
  },'${glamorId}', '${provider}','${JSON.stringify(props)}');
  </script>
  `;
};

and in my client webpack file, I have this config added in webpack

output: {
    path: dist,
    filename: `[name].[contenthash]-${ASSET_VERSION}.js`
    //NOTE: publicPath is dynamically set at runtime via WEBPACK_PUBLIC_PATH env var on the rails server
  },

The issue that I am facing is,

the file created in my out folder has the [name].[contenthash]-${ASSET_VERSION}.js filename while the script tags that are imported only have '[name].js'.

Thus in network tab I'm getting 404 errors

radha-bacancy avatar Nov 24 '20 16:11 radha-bacancy