Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,009
Community Members
 
Community Events
184
Community Groups

atlassian-connect-express: furl error

Hello! In this article we will continue to discover atlassian-connect-express.

This article is based on this article.

You can watch a video for this article here.

You can find the final source code here.

Problem

Run the app and open the Example page menu item in one of your repos:

Screenshot 2021-04-24 at 08.21.21.png

The page works. But if you have a look in the ngrok tunnel you will see an error:

Screenshot 2021-04-24 at 08.22.50.png

It means that our addon.js could not be found. If you we have a look at our source code we will find public/js/addon.js.

Let’s add alert(“my message”); line to the file. It will provide an alert if our addon.js file was loaded:

alert("my message");
AP.require('request', function(request) {
    request({
        url: '/2.0/user/',
        success: function(data) {
            $('#displayName')
                .text(data.display_name)
                .next('.loading').hide();
        }
    });
});

Now if you open again the Example Page you will not see the alert.

Let’s fix the problem.

Fix the problem

Have a look at views/layout.hbs, you will see the following line:

  <script src="{{furl '/js/addon.js'}}"></script>

It means that we get the path to /js/addon.js via a function called furl.

This function is defined in app.js file:

// Mount the static files directory
// Anything in ./public is served up as static content
const staticDir = path.join(__dirname, 'public');
app.use(express.static(staticDir));

// Enable static resource fingerprinting for far future expires caching in production
app.use(expiry(app, {dir: staticDir, debug: devEnv}));

// Add an hbs helper to fingerprint static resource urls
hbs.registerHelper('furl', function(url){ return app.locals.furl(url); });

First, we define where all static files are and register the path. Then we connect static-expiry package to provide the app.locals.furl function. And then we register furl function for hbs templates. This furl function we use in our layout.hbs file.

Why do we need the static-expiry package at all?

Static-expiry provides two things:

  • A helper method furl for your templates in order to generate fingerprinted URLs for your static assets
  • Middleware to handle incoming requests for the fingerprinted URLs. It handles them by rewriting the url to it’s original value and setting appropriate conditional/unconditional cache headers according to your configuration.

In short furl function adds a random prefix to your static files, which means that every time you install your app, the name of your static resources is different, which means that the browser will reload your static file and will not use the files from the browser’s cache. Which means that your changes in static files will be visible for the browser and hence for the user.

But this feature does not work for us. What happened?

It is written in the docs:

static-expiry does not serve static assets. It is invoked by calling it’s function that returns the middleware. It should be placed just before the middleware you use for serving static assets.

You see it should be place before the middleware you use, but we placed it after. That is why nothing works. Let’s rewrite our app.js to this one:

const staticDir = path.join(__dirname, 'public');
// Enable static resource fingerprinting for far future expires caching in production
app.use(expiry(app, {dir: staticDir, debug: devEnv}));

// Mount the static files directory
// Anything in ./public is served up as static content
app.use(express.static(staticDir));

// Add an hbs helper to fingerprint static resource urls
hbs.registerHelper('furl', function(url){ return app.locals.furl(url); });

Now if you run your app, you will see the alert:

Screenshot 2021-04-24 at 08.36.01.png

That is all for the article!

1 comment

M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 10, 2021

Amazing article

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events