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.

How to add hash tag to static assets

See original GitHub issue

Some of my components refer the assets files in a css way:

import styled from 'styled-components';
const Logo = styled.div`
     background-image:  url('/static/logo.png'),
`

I want to use CDN for all the assets in /statics folder, it is possible to give a version tag( like BUILD_ID) to these files in production?

No clue, anyone has a advice?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
taichenglucommented, Aug 24, 2017

File: ./build-static.js

const fs = require('fs')
const path = require('path')
const execSync = require('child_process').execSync
const revHash = require('rev-hash')

const srcDir = 'static'
const distDir = 'buildstatic'
const hashData = {}

execSync(`rm -r -f ${distDir}`)

const buildStatic = (src, dist) => {
  fs.access(dist, (err) => {
    if (err) {
      fs.mkdirSync(dist)
    }

    initBuild(src, dist)
  })
}

const initBuild = (src, dist) => {
  const paths = fs.readdirSync(src)

  paths.forEach((_path) => {
    const subSrc = `${src}/${_path}`
    const subDist = `${dist}/${_path}`
    const stat = fs.statSync(subSrc)

    if (stat.isFile()) {
      const fileData = fs.readFileSync(subSrc)
      const pathObj = path.parse(subSrc)
      const hash = revHash(fileData)
      const hashDir = pathObj.dir.split(`${srcDir}/`)[1] || ''
      const hashKey = `${hashDir}/${pathObj.name}${pathObj.ext}`
      const filePath = `${path.parse(subDist).dir}/${pathObj.name}.${hash}${pathObj.ext}`

      if (pathObj.ext) {
        hashData[hashKey] = hash
        fs.writeFileSync(filePath, fileData)
      }
    } else if (stat.isDirectory()) {
      buildStatic(subSrc, subDist)
    }
  })

  fs.writeFileSync('static-hash.json', JSON.stringify(hashData))
}

buildStatic(srcDir, distDir)

run: node build-static.js

File: ./demo.js

import staticHash from 'static-hash'

const isPro = true

const staticRequire = (path) => {
  const hash = staticHash[path]
  const pathSplit = path.split('.')
  const fileName = pathSplit[0]
  const fileExt = pathSplit[1]
  const filePath = !isPro ? path : `${fileName}.${hash}.${fileExt}`

  let staticDir = '/static/'

  if (isPro) {
    staticDir = 'https://mycdn.com/static/'
  }

  return `${staticDir}${filePath}`
}

console.log(staticRequire('images/test.png'))

// <img src={staticRequire('images/test.png')} alt="" />

1reaction
faceacecommented, Jul 13, 2017

Same issue here. We tried using webpack file loader but seems not working for the current version of next.js.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add hash tag to static assets · Issue #2548 · vercel/next.js
For our project, we are working for images hash tags with next.js. It will be better to have the hash for every single...
Read more >
How to use Hashtags on Instagram Stories - Flick
So far we haven't talked a lot about how hashtags work on Instagram Stories, as opposed to IGTV, static feed images, or carousel...
Read more >
Introducing a brand new way to use #Hashtags! - dlvr.it support
1) Log into your dlvr.it account and click on the Feed to which you would like to add Hashtags. · 2) Click on...
Read more >
Use hashtags on Instagram | Instagram Help Centre
Click Write a caption... and type # followed by text or emoji (e.g. #flower). Click Share. If you want to add a hashtag...
Read more >
Add an image to a hashtag
You can add an image at the domain level of already defined hashtags if you have the admin role.
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